SlideShare a Scribd company logo
1Copyright © 2009 Brian Noyes, IDesign Inc
Brian Noyes
Chief Architect, IDesign
www.idesign.net
brian.noyes@idesign.net
Chief ArchitectChief Architect
IDesign Inc. (www.idesign.net)IDesign Inc. (www.idesign.net)
Microsoft Regional DirectorMicrosoft Regional Director
MVPMVP
Microsoft MVPMicrosoft MVP
Connected SystemsConnected Systems
About Brian PublishingPublishing
Developing Applications withDeveloping Applications with
Windows Workflow Foundation,Windows Workflow Foundation,
LiveLessons training DVD, June 2007LiveLessons training DVD, June 2007
Smart Client Deployment withSmart Client Deployment with
ClickOnce, Addison Wesley,ClickOnce, Addison Wesley,
January 2007January 2007
Data Binding in Windows Forms 2.0,Data Binding in Windows Forms 2.0,
Addison Wesley, January 2006Addison Wesley, January 2006
MSDN Magazine, MSDN Online,MSDN Magazine, MSDN Online,
CoDe Magazine, The Server Side .NET,CoDe Magazine, The Server Side .NET,
asp.netPRO, Visual Studio Magazineasp.netPRO, Visual Studio Magazine
SpeakingSpeaking
Microsoft TechEd US, Europe, Malaysia,Microsoft TechEd US, Europe, Malaysia,
Visual Studio Connections, DevTeach,Visual Studio Connections, DevTeach,
INETA Speakers Bureau, MSDNINETA Speakers Bureau, MSDN
WebcastsWebcasts
E-mail: brian.noyes@idesign.net
Blog: http://briannoyes.net
2Copyright © 2009 Brian Noyes, IDesign Inc
Agenda
Service Best Practices
Client Best Practices
Data Contracts
SOAP vs REST
ServiceDefinition
Separate contract from implementation
Contract (interface) first
Define services in a class library, not directly in a
host project
Layering
Separate Service Layer?
Instance model
Change to Per Call as default
Session / Singleton when?
3Copyright © 2009 Brian Noyes, IDesign Inc
ServiceException Handling
For operation specific exceptions
Try/catch, throw FaultException<T>
Favor using FaultException<T>
FaultException can be ambiguous to the client because
unhandled exceptions arrive as a FaultException
Include FaultContract in service contract definition
if you throw FaultExceptions
Part of the API you are exposing
For global exception handling from services
Use an error handler
Include exception details in debug builds only
ServiceSecurity
Intranet services
Default Windows Auth may be all you need
Possibly Hybrid – Windows Creds / Custom Application
Roles
Extranet / Internet / Custom security needs
Use ASP.NET Membership / Role providers
ASP.NET providers
Really a standard .NET framework security infrastructure
Built in providers for Windows or SQL Server – based
credentials / roles
Easy to implement custom providers
Establish principal on the thread
Re-usable across ASP.NET, WCF, WPF & Windows Forms (via
Client Application Services)
4Copyright © 2009 Brian Noyes, IDesign Inc
ServiceHosting
Favor WAS Hosting when Server 2008 is an option
Multiple protocol support
IIS Hosting model and tools
Favor IIS for external HTTP only services
Better on-box scalability / availability through worker
process model
Better management tools
Favor self-hosting for stateful services, callbacks, .NET
Service Bus, debugging
Have a console-based debug self-host for development
time
Can be a Windows Service project that is used for
production self-hosting with a mode switch for debugging
Consider Dublin hosting in the future
SelfHost Code
Do not put ServiceHost in a using statement in
production code
Dispose can throw an exception that masks the real
exception thrown from Open call
Explicitly call Close in try/catch, log/ deal with
exception in catch
5Copyright © 2009 Brian Noyes, IDesign Inc
Agenda
Service Best Practices
Client Best Practices
Data Contracts
SOAP vs REST
ClientProxy Classes
Favor static proxy class over ChannelFactory
Connection caching in the base class in 3.5
Place for encapsulation of common patterns
Hand-code or micro-code generate proxy
classes for internal services
Less bloated code
Share service contract and data contracts through
libraries
Explicit control over config file
6Copyright © 2009 Brian Noyes, IDesign Inc
ClientProxy Classes
Add Service Reference for external services or
when you want an async API on the client
Clean up config after it destroys it
Make sure to add references to data contract
libraries before adding the service reference to
avoid duplicate definitions
Live with the duplicate service contract definition
instead of needing to repeatedly clean up the proxy
code
ClientProxy Management
Cache client proxies if frequent calls to avoid
session establishment cost
If secure / reliable session enabled
Have to deal more cautiously with faulted proxies
Check proxy state before using
Get rid of proxy after exception
Don’t put proxies in a using statement
Dispose call might throw exception and mask real
exception
Explicitly close in a try/catch block, and if Close throws
an exception, Abort the proxy to ensure resource clean
up
7Copyright © 2009 Brian Noyes, IDesign Inc
ClientException Management
All exceptions thrown from a service call derive
from CommunicationException
FaultException could be wrapped unhandled
exception on the client, or explicit error returned
from the service
FaultException<T> always an explicit error returned
from the service
Simple approach:
Any exception from a proxy call, safe close the proxy
Advanced approach:
FaultException<T> - proxy is reusable
Agenda
Service Best Practices
Client Best Practices
Data Contracts
SOAP vs REST
8Copyright © 2009 Brian Noyes, IDesign Inc
Data Contracts
Favor data contracts over serializable types
More explicit model, better control over what the
client sees
Implement IExtensibleDataObject
Avoids dropping data that the service / client does
not understand
Avoid passing complex .NET specific types for
interoperable services
DataSets and Exception types
Data Contracts
Avoid XmlSerializer and MessageContracts
except for interoperable scenarios and REST
services
9Copyright © 2009 Brian Noyes, IDesign Inc
Agenda
Service Best Practices
Client Best Practices
Data Contracts
SOAP vs REST
SOAP vs REST
Favor SOAP services when you are writing a
service that only your code will consume
Favor REST services for publicly exposed, data
oriented services
10Copyright © 2009 Brian Noyes, IDesign Inc
Resources
IDesign WCF Master Class
IDesign WCF Coding Standard – http://www.idesign.net
Programming WCF, Juval Lowy, O’Reilly & Associates, 2007
Learning WCF, Michele Leroux Bustamante, O’Reilly & Associates, 2007
Connect Apps with WCF, Brian Noyes, Visual Studio Magazine, Feb 2008,
http://visualstudiomagazine.com/articles/2008/02/01/connect-apps-with-
wcf.aspx?sc_lang=en
Wenlong Dong’s Blog: http://blogs.msdn.com/wenlong/default.aspx
E-mail: brian.noyes@idesign.net
Blog: http://briannoyes.net

More Related Content

PPTX
VDISecurity.org Overview
PPTX
DerbyCon 7 - Hacking VDI, Recon and Attack Methods
PDF
SAML and Other Types of Federation for Your Enterprise
PDF
Présentation webinar Ivanti - Analyse du Patch Tuesday - Janvier 2020
PPTX
10 Tricks and Tips for WCF
PPTX
Advanced WCF Workshop
PPTX
Windows Communication Foundation (WCF) Best Practices
DOC
WCF tutorial
VDISecurity.org Overview
DerbyCon 7 - Hacking VDI, Recon and Attack Methods
SAML and Other Types of Federation for Your Enterprise
Présentation webinar Ivanti - Analyse du Patch Tuesday - Janvier 2020
10 Tricks and Tips for WCF
Advanced WCF Workshop
Windows Communication Foundation (WCF) Best Practices
WCF tutorial

Similar to Wcf best practices (20)

PDF
WCF Multi Layer Services Development with Entity Framework 4th Edition Editio...
PDF
WCF Multi Layer Services Development with Entity Framework 4th Edition Editio...
PDF
Wcf 40 Multitier Services Development With Linq To Entities Mike Liu
PDF
Wcf 40 Multitier Services Development With Linq To Entities Mike Liu
PPTX
Latest trends in information technology
PPT
PDC Highlights
PDF
Connected Applications using WF and WCF
PPT
Designingapplswithnet
PPTX
Tales from the trenches creating complex distributed systems
PPTX
Web services
PDF
SOA and WCF (Windows Communication Foundation) basics
DOCX
PPTX
Smart Client Software Factory 2010
PPT
A Software Factory Integrating Rational & WebSphere Tools
PPTX
Pragmatic Approach to Microservices and Cell-based Architecture
PPT
A Software Factory Integrating Rational Team Concert and WebSphere tools
PDF
Architecting a Large Software Project - Lessons Learned
PDF
Cert05 70-487 - developing microsoft azure and web services
PPTX
What is a SOA Service - from the Business Perspective? How detailed, what le...
WCF Multi Layer Services Development with Entity Framework 4th Edition Editio...
WCF Multi Layer Services Development with Entity Framework 4th Edition Editio...
Wcf 40 Multitier Services Development With Linq To Entities Mike Liu
Wcf 40 Multitier Services Development With Linq To Entities Mike Liu
Latest trends in information technology
PDC Highlights
Connected Applications using WF and WCF
Designingapplswithnet
Tales from the trenches creating complex distributed systems
Web services
SOA and WCF (Windows Communication Foundation) basics
Smart Client Software Factory 2010
A Software Factory Integrating Rational & WebSphere Tools
Pragmatic Approach to Microservices and Cell-based Architecture
A Software Factory Integrating Rational Team Concert and WebSphere tools
Architecting a Large Software Project - Lessons Learned
Cert05 70-487 - developing microsoft azure and web services
What is a SOA Service - from the Business Perspective? How detailed, what le...
Ad

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Introduction to Artificial Intelligence
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
assetexplorer- product-overview - presentation
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Softaken Excel to vCard Converter Software.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Internet Downloader Manager (IDM) Crack 6.42 Build 41
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Computer Software and OS of computer science of grade 11.pptx
Odoo POS Development Services by CandidRoot Solutions
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
CHAPTER 2 - PM Management and IT Context
Why Generative AI is the Future of Content, Code & Creativity?
Design an Analysis of Algorithms I-SECS-1021-03
Introduction to Artificial Intelligence
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
history of c programming in notes for students .pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
assetexplorer- product-overview - presentation
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Softaken Excel to vCard Converter Software.pdf
Ad

Wcf best practices

  • 1. 1Copyright © 2009 Brian Noyes, IDesign Inc Brian Noyes Chief Architect, IDesign www.idesign.net brian.noyes@idesign.net Chief ArchitectChief Architect IDesign Inc. (www.idesign.net)IDesign Inc. (www.idesign.net) Microsoft Regional DirectorMicrosoft Regional Director MVPMVP Microsoft MVPMicrosoft MVP Connected SystemsConnected Systems About Brian PublishingPublishing Developing Applications withDeveloping Applications with Windows Workflow Foundation,Windows Workflow Foundation, LiveLessons training DVD, June 2007LiveLessons training DVD, June 2007 Smart Client Deployment withSmart Client Deployment with ClickOnce, Addison Wesley,ClickOnce, Addison Wesley, January 2007January 2007 Data Binding in Windows Forms 2.0,Data Binding in Windows Forms 2.0, Addison Wesley, January 2006Addison Wesley, January 2006 MSDN Magazine, MSDN Online,MSDN Magazine, MSDN Online, CoDe Magazine, The Server Side .NET,CoDe Magazine, The Server Side .NET, asp.netPRO, Visual Studio Magazineasp.netPRO, Visual Studio Magazine SpeakingSpeaking Microsoft TechEd US, Europe, Malaysia,Microsoft TechEd US, Europe, Malaysia, Visual Studio Connections, DevTeach,Visual Studio Connections, DevTeach, INETA Speakers Bureau, MSDNINETA Speakers Bureau, MSDN WebcastsWebcasts E-mail: brian.noyes@idesign.net Blog: http://briannoyes.net
  • 2. 2Copyright © 2009 Brian Noyes, IDesign Inc Agenda Service Best Practices Client Best Practices Data Contracts SOAP vs REST ServiceDefinition Separate contract from implementation Contract (interface) first Define services in a class library, not directly in a host project Layering Separate Service Layer? Instance model Change to Per Call as default Session / Singleton when?
  • 3. 3Copyright © 2009 Brian Noyes, IDesign Inc ServiceException Handling For operation specific exceptions Try/catch, throw FaultException<T> Favor using FaultException<T> FaultException can be ambiguous to the client because unhandled exceptions arrive as a FaultException Include FaultContract in service contract definition if you throw FaultExceptions Part of the API you are exposing For global exception handling from services Use an error handler Include exception details in debug builds only ServiceSecurity Intranet services Default Windows Auth may be all you need Possibly Hybrid – Windows Creds / Custom Application Roles Extranet / Internet / Custom security needs Use ASP.NET Membership / Role providers ASP.NET providers Really a standard .NET framework security infrastructure Built in providers for Windows or SQL Server – based credentials / roles Easy to implement custom providers Establish principal on the thread Re-usable across ASP.NET, WCF, WPF & Windows Forms (via Client Application Services)
  • 4. 4Copyright © 2009 Brian Noyes, IDesign Inc ServiceHosting Favor WAS Hosting when Server 2008 is an option Multiple protocol support IIS Hosting model and tools Favor IIS for external HTTP only services Better on-box scalability / availability through worker process model Better management tools Favor self-hosting for stateful services, callbacks, .NET Service Bus, debugging Have a console-based debug self-host for development time Can be a Windows Service project that is used for production self-hosting with a mode switch for debugging Consider Dublin hosting in the future SelfHost Code Do not put ServiceHost in a using statement in production code Dispose can throw an exception that masks the real exception thrown from Open call Explicitly call Close in try/catch, log/ deal with exception in catch
  • 5. 5Copyright © 2009 Brian Noyes, IDesign Inc Agenda Service Best Practices Client Best Practices Data Contracts SOAP vs REST ClientProxy Classes Favor static proxy class over ChannelFactory Connection caching in the base class in 3.5 Place for encapsulation of common patterns Hand-code or micro-code generate proxy classes for internal services Less bloated code Share service contract and data contracts through libraries Explicit control over config file
  • 6. 6Copyright © 2009 Brian Noyes, IDesign Inc ClientProxy Classes Add Service Reference for external services or when you want an async API on the client Clean up config after it destroys it Make sure to add references to data contract libraries before adding the service reference to avoid duplicate definitions Live with the duplicate service contract definition instead of needing to repeatedly clean up the proxy code ClientProxy Management Cache client proxies if frequent calls to avoid session establishment cost If secure / reliable session enabled Have to deal more cautiously with faulted proxies Check proxy state before using Get rid of proxy after exception Don’t put proxies in a using statement Dispose call might throw exception and mask real exception Explicitly close in a try/catch block, and if Close throws an exception, Abort the proxy to ensure resource clean up
  • 7. 7Copyright © 2009 Brian Noyes, IDesign Inc ClientException Management All exceptions thrown from a service call derive from CommunicationException FaultException could be wrapped unhandled exception on the client, or explicit error returned from the service FaultException<T> always an explicit error returned from the service Simple approach: Any exception from a proxy call, safe close the proxy Advanced approach: FaultException<T> - proxy is reusable Agenda Service Best Practices Client Best Practices Data Contracts SOAP vs REST
  • 8. 8Copyright © 2009 Brian Noyes, IDesign Inc Data Contracts Favor data contracts over serializable types More explicit model, better control over what the client sees Implement IExtensibleDataObject Avoids dropping data that the service / client does not understand Avoid passing complex .NET specific types for interoperable services DataSets and Exception types Data Contracts Avoid XmlSerializer and MessageContracts except for interoperable scenarios and REST services
  • 9. 9Copyright © 2009 Brian Noyes, IDesign Inc Agenda Service Best Practices Client Best Practices Data Contracts SOAP vs REST SOAP vs REST Favor SOAP services when you are writing a service that only your code will consume Favor REST services for publicly exposed, data oriented services
  • 10. 10Copyright © 2009 Brian Noyes, IDesign Inc Resources IDesign WCF Master Class IDesign WCF Coding Standard – http://www.idesign.net Programming WCF, Juval Lowy, O’Reilly & Associates, 2007 Learning WCF, Michele Leroux Bustamante, O’Reilly & Associates, 2007 Connect Apps with WCF, Brian Noyes, Visual Studio Magazine, Feb 2008, http://visualstudiomagazine.com/articles/2008/02/01/connect-apps-with- wcf.aspx?sc_lang=en Wenlong Dong’s Blog: http://blogs.msdn.com/wenlong/default.aspx E-mail: brian.noyes@idesign.net Blog: http://briannoyes.net