Windows Communication Foundation (WCF) Best Practices

19,736 views

Published on

Published in: Education, Technology

Windows Communication Foundation (WCF) Best Practices

  1. 1. 28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices<br />
  2. 2. Don't build a WCF service !<br />Unless you have a good reason for it:<br />Security reasons (business Logic in perimeter network)<br />Shared business logic (multiple apps or non .NET apps)<br />Heavy resource consumption (long-running?)<br />Separate maintainable<br />Separate scalable<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />2<br />
  3. 3. Know the tools!<br />Svcutil.exe<br />SvcConfigEditor.exe <br />SvcTraceViewer.exe <br />REST starter kit<br />AppFabric<br />Web Services Software Factory (WSSF) <br />http://servicefactory.codeplex.com/<br />Enterprise Library (EntLib)<br />Exception Handling Application Block<br />Logging Application Block<br />Validation Application Block<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />3<br />
  4. 4. Additional tools on CodePlex<br />http://wcfloadtest.codeplex.com<br />takes a WCF trace file and a WCF client proxy, or a WCF interface contract, and generates a C# unit test that replays the same sequence of calls found in the trace file. The unit test can then be used to load-test the target <br />http://wcfcontrib.codeplex.com/<br />library with infrastructure implementations for client and service communications<br />http://wcfsecurity.codeplex.com/<br />http://wcfextensions.codeplex.com/<br />WCF Compression Channel<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />4<br />
  5. 5. Service best practices<br />Separate contract from implementation<br />Contract (interface) first (http://wscfblue.codeplex.com/)<br />Define services in a class library, not directly in a host project<br />Layering<br />Separate Service Layer<br />Instance model<br />Change to Per Call as default<br />Session / Singleton – use only when needed<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />5<br />
  6. 6. Exception Handling (I)<br />For operation specific exceptions<br />Try/catch, throw FaultException<T><br />Favor using FaultException<T><br />FaultException can be ambiguous to the client because unhandled exceptions arrive as a FaultException<br />Include FaultContract in service contract definition<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />6<br />
  7. 7. Exception Handling (II)<br />If you throw FaultExceptions<br />Part of the API you are exposing<br />For global exception handling from services<br />Use an error handler<br />Include exception details in debug builds only !!<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />7<br />
  8. 8. Hosting<br />Favor WAS Hosting when on Windows Server 2008<br />Multiple protocol support<br />IIS Hosting model and tools<br />Use IIS hosting for external HTTP only<br />Favor self-hosting for stateful services, callbacks, .NET Service Bus, debugging<br />Have a console-based debug self-host for development time<br />Can be a Windows Service project that is used for production self-hosting with a mode switch for debugging<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />8<br />
  9. 9. When you self host … <br />Do not put ServiceHost in a using statement in production code<br />Dispose can throw an exception that masks the real exception thrown from Open call<br />Explicitly call Close in try/catch and log/ deal with exception in catch<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />9<br />
  10. 10. Client Proxy Classes (I)<br />Use static proxy class when possible instead of ChannelFactory<br />Connection caching in the base class in 3.5<br />Place for encapsulation of common patterns<br />Hand-code or generate proxy classes for internal services<br />Less bloated code<br />Share service contract and data contracts through libraries<br />Explicit control over config file<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />10<br />
  11. 11. Client Proxy Classes (II)<br />Add Service Reference for external services or when you want an async API on the client<br />Clean up config after it messes it up<br />Make sure to add references to data contract libraries before adding the service reference to avoid duplicate definitions<br />Live with the duplicate service contract definition instead of needing to repeatedly clean up the proxy code<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />11<br />
  12. 12. Client Proxy Management<br />Cache client proxies if frequent calls to avoid session establishment cost<br />If secure / reliable session enabled<br />Have to deal more cautiously with faulted proxies<br />Check proxy state before using<br />Get rid of proxy after exception<br />Don’t put proxies in a using statement<br />Dispose call might throw exception and mask real exception<br />Explicitly close in a try/catch block, and if Close throws an exception, Abort the proxy to ensure resource clean up<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />12<br />
  13. 13. Client Exception Management<br />All exceptions thrown from a service call derive from CommunicationException<br />FaultException<T> always an explicit error returned<br />from the service<br />Simple approach:<br />Any exception from a proxy call, safe close the proxy<br />Advanced approach:<br />FaultException<T> - proxy is reusable<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />13<br />
  14. 14. Data Contracts<br />Avoid XmlSerializer and MessageContracts except for interoperable scenarios and REST services<br />Favor data contracts over serializable types<br />More explicit model, better control over what the client sees<br />Implement IExtensibleDataObject<br />Avoids dropping data that the service / client does not understand<br />Avoid passing complex .NET specific types for interoperable services<br />DataSets and Exception types<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />14<br />
  15. 15. SOAP vs REST<br />Favor SOAP services when you are writing a service that only your code will consume<br />Favor REST services for publicly exposed, data oriented services<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />15<br />
  16. 16. Don't forget XmlNamespace settings<br />By default WCF puts the “http://tempuri.org/”  namespace in your wsdl and contracts. Normally you don’t want this. You can control this from three places:<br />In your config file in the services/service/endpoint/bindingNamespaceattribute<br />In your servicecontract: [ServiceContract(Namespace="http://myuri.net/")]  <br />In your servicebehavior: [ServiceBehavior(Namespace="http://myuri.net/")]<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />16<br />
  17. 17. More subtle things with WCF <br />One-way !?<br />One-way is not always really one-way<br />even when doing oneway, closing a client can block until the service side is done<br />this is happening in quite some cases where bindings are configured to maintain sessions. <br />it happens for example with Message security and or reliable session support (WS-ReliableMessaging).<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />17<br />
  18. 18. The limits of reliable Messaging with WCF (I)<br />Messages still can get lost with this mechanism if for example <br />the service is shutdown <br />IIS reset<br />If you need end-to-end durable reliable messaging with full support for transactional I/O you need an infrastructure that's in control of both ends of the communication <br />NetMsmqBinding. <br />MSMQ messages are limited to 4 MB<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />18<br />
  19. 19. The limits of reliable Messaging with WCF (II)<br />If you are mostly worried about not losing messages under less-than-optimal networking conditions or require session support<br />the reliable session support in WCF is the right choice<br />isn’t very handy with load balancers<br />Sticky session/session affinity<br />28 July, 2011<br />Windows Communication Foundation (WCF) Best Practices <br />19<br />
  20. 20. 20<br />Windows Communication Foundation (WCF) Best Practices <br />28 July, 2011<br />

×