WCF Best Practices – Easy WCF
Skip to content

WCF Best Practices

In this article, we will learn:

  • Why to build WCF Service?
  • Know the Required Tools
  • Best Practices
    • WCF Designs
    • Bindings
    • Contracts
    • Hosting
    • Exception Handlings
    • Proxy Management
    • Performance

Why to build WCF Service?

Build WCF service if you have a good reason for it:

  • Business logic in perimeter area and there are security issues
  • Business logic is shared between multiple applications or non .net applications
  • Heavy Resource consumption

Know the Required Tools?

  • SvcUtil.exe
  • SvcConfigEditor.exe
  • SvcTraceViewer.exe
  • RestStarter kit
  • AppFabric
  • Web Services Software Factory (WSSF)
  • Enterprise Library (EntLib)

 

Best Practices in WCF Designs:

WCF Design Guidelines:

  • Separate service contracts from service implementation.
  • Decorate an interface using the ServiceContract attribute and refrain from specifying this attribute on a class, i.e., your service contracts should be interfaces and not classes.
  • Define services in class library not directly in host project
  • Manipulate the service headers to specify metadata like, culture, session data, etc.
  • Leverage the ServiceContract, DataContract, ServiceBehavior attributes to decorate your types and facilitate versioning of your service methods.
  • Use data transfer objects to exchange data back and forth between the service and the domain objects
  • Decouple the WCF configuration metadata from the web.config file to facilitate editing the WCF bindings, behaviors, etc. for each environment your service is supposed to work on
  • Use Session/Singleton Instance model when required and make percall as default

 

WCF Binding Guidelines:

WCF Bindings

 

WCF Contracts Guidelines:

  • Service Contract:
    • Use the Name, Namespace parameters on all contracts and also use the Order parameter on all the data members
    • If you would like to remove a service operation or update a service operation, refrain from changing the existing one rather, create new ones.
    • Use the async programming pattern when you are calling WCF services to ensure that your user interface can work in a non-blocking mode.
    • Use RESTful services when you would like to build light-weight and scalable WCF services that would leverage the HTTP protocol
  • Data Contract:
    • Avoid XMLSerializer and MessageContracts excepts for Interoperable scenarios and Rest services
    • Avoid passing complex .net specific types for Interoperable servives

 

WCF Hosting Guidelines:

  • WAS Hosting on Windows server 2008
  • IIS Hosting for external HTTP only
  • Self Hosting for stateful services, callback and .net service Bus
  • Console based debug self host for development time

 

WCF Exception Handling Guidelines:

  • For operation Specific Exceptions,
    • Try/Catch, throw FaultException<T>
    • FaultException can be ambiguous  to client because unhandled exceptions arrive as Fault exception
  • Include Fault contract in Service Contract definition
  • For global exception handling from services, use an error handler
  • Define exception details in debug build only

 

WCF Proxy Guidelines:

  • Proxy is best when your service is used by multiple applications
  • Use static proxy class when possible instead of ChannelFactory
  • Add service references for external services or when you want an async API on client
  • Add references to data contract libraries before adding the  service reference to avoid duplication definition

 

Performance Guidelines:

  • If your WCF service is hosted on IIS you can turn on IIS compression for static and dynamic content to improve performance.
  •  You can use the Windows Performance Monitor to analyse the performance bottlenecks. You can enable WCF performance counters in config file
  • Take advantage of NetTCP binding to boost the throughput of your WCF services.
  • Use light-weight data exchange formats, i.e., JSON in your WCF service to minimize the network traffic that your WCF service would otherwise need to consume if SOAP were used as a data exchange format.
  • You should always use DataContract Serializer in lieu of XMLSerializer for better performance.
  • You can also explore the possibilities of using binary serialization formats like MessagePack to minimize the traffic even further.
  • You can also compress the data transferred over the wire to cut down on the network traffic between the service provider and the service consumers.
  • Since there is no built in way to cache service responses in WCF, you can write custom code to cache service responses.
  • You should set WCF service throttling and service configuration appropriately to improve service performance

 

Please share you experiences on WCF for other Viewers to follow. Thanks for visiting !!

 

 

 

 

© 2015, admin. All rights reserved.

One thought on “WCF Best Practices

  1. Pingback: Five steps to optimize WCF Service | Easy WCF

Leave a Reply

Your email address will not be published. Required fields are marked *