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

Similar to Wcf best practices

Defy Occassionally Connected Challenges With Smart Client Applications
Defy Occassionally Connected Challenges With Smart Client ApplicationsDefy Occassionally Connected Challenges With Smart Client Applications
Defy Occassionally Connected Challenges With Smart Client Applications
Clint Edmonson
 
Migration of Microsoft Workloads to AWS
Migration of Microsoft Workloads to AWSMigration of Microsoft Workloads to AWS
Migration of Microsoft Workloads to AWS
Amazon Web Services
 
Adding the Sec to Your DevOps Pipelines
Adding the Sec to Your DevOps PipelinesAdding the Sec to Your DevOps Pipelines
Adding the Sec to Your DevOps Pipelines
Amazon Web Services
 
Cloud 12 08 V2
Cloud 12 08 V2Cloud 12 08 V2
Cloud 12 08 V2
Pini Cohen
 
Ws08 R2 Itpro Session 2 Technical Overview Part2
Ws08 R2 Itpro Session 2 Technical Overview Part2Ws08 R2 Itpro Session 2 Technical Overview Part2
Ws08 R2 Itpro Session 2 Technical Overview Part2chenley
 
W7 Enterprise
W7 EnterpriseW7 Enterprise
W7 Enterprisearalves
 
W7 for IT Professionals
W7 for IT ProfessionalsW7 for IT Professionals
W7 for IT Professionalsguest632c73
 
visual basic 2005 programmer certification
visual basic 2005 programmer certificationvisual basic 2005 programmer certification
visual basic 2005 programmer certification
Vskills
 
規劃大規模遷移到 AWS 的最佳實踐
規劃大規模遷移到 AWS 的最佳實踐規劃大規模遷移到 AWS 的最佳實踐
規劃大規模遷移到 AWS 的最佳實踐
Amazon Web Services
 
Resume ujjwal
Resume ujjwalResume ujjwal
Resume ujjwal
Ujjwal chatterjee
 
Web Hosting for Web Designers and Developers
Web Hosting for Web Designers and DevelopersWeb Hosting for Web Designers and Developers
Web Hosting for Web Designers and Developers
goodfriday
 
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Amazon Web Services
 
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Roven Drabo
 
AWS X-Ray: Debugging Applications at Scale - AWS Online Tech Talks
AWS X-Ray: Debugging Applications at Scale - AWS Online Tech TalksAWS X-Ray: Debugging Applications at Scale - AWS Online Tech Talks
AWS X-Ray: Debugging Applications at Scale - AWS Online Tech Talks
Amazon Web Services
 
Getting Started with AWS
Getting Started with AWSGetting Started with AWS
Getting Started with AWS
Amazon Web Services
 
Securing Servers in Public and Hybrid Clouds
Securing Servers in Public and Hybrid CloudsSecuring Servers in Public and Hybrid Clouds
Securing Servers in Public and Hybrid Clouds
RightScale
 
1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).ppt1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).pptwebhostingguy
 
1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).ppt1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).pptwebhostingguy
 
WebFest 2011 Hosting Applications CR by David Tang
WebFest 2011 Hosting Applications CR by David TangWebFest 2011 Hosting Applications CR by David Tang
WebFest 2011 Hosting Applications CR by David TangSpiffy
 
Windows 7 for IT Professionals
Windows 7 for IT ProfessionalsWindows 7 for IT Professionals
Windows 7 for IT ProfessionalsRishu Mehra
 

Similar to Wcf best practices (20)

Defy Occassionally Connected Challenges With Smart Client Applications
Defy Occassionally Connected Challenges With Smart Client ApplicationsDefy Occassionally Connected Challenges With Smart Client Applications
Defy Occassionally Connected Challenges With Smart Client Applications
 
Migration of Microsoft Workloads to AWS
Migration of Microsoft Workloads to AWSMigration of Microsoft Workloads to AWS
Migration of Microsoft Workloads to AWS
 
Adding the Sec to Your DevOps Pipelines
Adding the Sec to Your DevOps PipelinesAdding the Sec to Your DevOps Pipelines
Adding the Sec to Your DevOps Pipelines
 
Cloud 12 08 V2
Cloud 12 08 V2Cloud 12 08 V2
Cloud 12 08 V2
 
Ws08 R2 Itpro Session 2 Technical Overview Part2
Ws08 R2 Itpro Session 2 Technical Overview Part2Ws08 R2 Itpro Session 2 Technical Overview Part2
Ws08 R2 Itpro Session 2 Technical Overview Part2
 
W7 Enterprise
W7 EnterpriseW7 Enterprise
W7 Enterprise
 
W7 for IT Professionals
W7 for IT ProfessionalsW7 for IT Professionals
W7 for IT Professionals
 
visual basic 2005 programmer certification
visual basic 2005 programmer certificationvisual basic 2005 programmer certification
visual basic 2005 programmer certification
 
規劃大規模遷移到 AWS 的最佳實踐
規劃大規模遷移到 AWS 的最佳實踐規劃大規模遷移到 AWS 的最佳實踐
規劃大規模遷移到 AWS 的最佳實踐
 
Resume ujjwal
Resume ujjwalResume ujjwal
Resume ujjwal
 
Web Hosting for Web Designers and Developers
Web Hosting for Web Designers and DevelopersWeb Hosting for Web Designers and Developers
Web Hosting for Web Designers and Developers
 
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
 
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
Patterns and Considerations in Service Discovery - Con327 - re:Invent 2017
 
AWS X-Ray: Debugging Applications at Scale - AWS Online Tech Talks
AWS X-Ray: Debugging Applications at Scale - AWS Online Tech TalksAWS X-Ray: Debugging Applications at Scale - AWS Online Tech Talks
AWS X-Ray: Debugging Applications at Scale - AWS Online Tech Talks
 
Getting Started with AWS
Getting Started with AWSGetting Started with AWS
Getting Started with AWS
 
Securing Servers in Public and Hybrid Clouds
Securing Servers in Public and Hybrid CloudsSecuring Servers in Public and Hybrid Clouds
Securing Servers in Public and Hybrid Clouds
 
1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).ppt1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).ppt
 
1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).ppt1 - Why Host on Windows (28).ppt
1 - Why Host on Windows (28).ppt
 
WebFest 2011 Hosting Applications CR by David Tang
WebFest 2011 Hosting Applications CR by David TangWebFest 2011 Hosting Applications CR by David Tang
WebFest 2011 Hosting Applications CR by David Tang
 
Windows 7 for IT Professionals
Windows 7 for IT ProfessionalsWindows 7 for IT Professionals
Windows 7 for IT Professionals
 

Recently uploaded

Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
Game Development with Unity3D (Game Development lecture 3)
Game Development  with Unity3D (Game Development lecture 3)Game Development  with Unity3D (Game Development lecture 3)
Game Development with Unity3D (Game Development lecture 3)
abdulrafaychaudhry
 

Recently uploaded (20)

Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
Game Development with Unity3D (Game Development lecture 3)
Game Development  with Unity3D (Game Development lecture 3)Game Development  with Unity3D (Game Development lecture 3)
Game Development with Unity3D (Game Development lecture 3)
 

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