A Deep Dive into Best Practices with the Windows Communication Foundation John Justice COM415 Program Manager Microsoft Corporation
What we’re covering today: 10 – Choose a Binding that Fits Your Needs 6 – Stay Flexible with Config 6 – Stay Flexible with Config 5 – Send Faults, not Exceptions 5 – Send Faults, not Exceptions 4 – Avoid UI Threading Conflicts 4 – Avoid UI Threading Conflicts 3 – Use UDDI Internally 3 – Use UDDI Internally 2 – Go Beyond XML Interoperability 2 – Go Beyond XML Interoperability 1 – Understand the Service Lifecycle 1 – Understand the Service Lifecycle
10: Choose a Binding that Fits Your Needs
BasicHttpBindingWsHttpBindingWsDualHttpBinding Bindings: Out of the Box NetTcpBindingNetNamedPipeBindingNetMsmqBindingNetPeerTcpBinding Interoperable WCF to WCF
Bindings: Decision Tree BasicHttpBinding WsHttpBinding Interop? What Level? Duplex? WSDualHttpBinding NetNamedPipeBinding NetMsmqBinding NetPeerTcpBinding NetTcpBinding No Basi c No Yes Local? Need Queues? Peer to Peer? No No No Yes Yes Yes WS Yes
Bindings
6: Stay Flexible with Config
Config: Range of options Deployment Flexibility All Code All Config End-user apps Enterprise apps
Config: When you need to lock down… Lock down only what you must Check your requirements Make sure your checks are being executed Write a custom ValidatorWrite a custom Validator Create your own BindingCreate your own Binding Require your Validator with in machine.configRequire your Validator with in machine.config Use a custom ServiceHostUse a custom ServiceHost Note: A malicious programmer can circumvent any of these options in code.
Stay Flexible with Config
5: Send Faults, not Exceptions
Send Faults, Not Exceptions Faults are at the contract level Don’t shove implementation level exceptions into your Faults When you get into trouble, send business level faults, not exceptions Indigo makes defining and sending custom faults easy
Custom Faults
4: Avoid UI Threading Conflicts
Avoid UI Threading Conflicts Windows Forms and Windows Presentation Foundation apps use the main program thread for updates WCF services use the main program thread for processing requests If you host a WCF service (or duplex client) in a Windows Forms or WPF app, you’ll need to be careful
Windows Forms Threading
3: Use UDDI Internally
Use UDDI Internally UDDI isn’t just for public registries When you need a layer of Web service indirection, think UDDI Development -> Test -> Staging -> Production Choosing between non-clustered services
UDDI
2: Go beyond XML Interoperability
Interop: Send Useful XML Infrastructure interoperability: == The plumbing can deliver your XML Being kind to your consumers: == There’s a good programming experience on both sides of the wire Use a practical, platform neutral subset of XML Schema Follow solid engineering practices when designing your contracts
Interop: Contract Guidelines Use our work: prototype with DataContract Use simple types that map well between.NET and Java DataSet is great for.NET to.NET, but isn’t supported well by other vendors Use typed, self-contained complex types When writing XSD-first: Avoid Avoid
1: Understand the Service Lifecycle
Service Lifecycle: Primary players Channel Lives for lifetime of channel sessionLives for lifetime of channel session Holds security informationHolds security information Holds Reliable Session IDHolds Reliable Session ID InstanceContext Provides connection between Channel and Service objectProvides connection between Channel and Service object Controls instance sharing and lifetimeControls instance sharing and lifetime Service Your object that implements the contractYour object that implements the contract
Service Lifecycle: Channel session Channel Lifetime: Lives for the duration of the channel sessionLifetime: Lives for the duration of the channel session Channel lifetime varies by transportChannel lifetime varies by transport InstanceContextService What’s the session lifetime of my channel? HTTP One Operation TCP Connection MSMQ One Operation Turn on Reliable Sessions to extend the session lifetime
Service Lifecycle: Channel session Channel Turn on Reliable Sessions to extend the session lifetime InstanceContextService </binding> </bindings>
Service Lifecycle: Channel session Channel Get finer-grained control with IsInitiating() and IsTerminating InstanceContextService // Close the Channel after calling [OperationContract(IsTerminating=true)] public String Echo(String input);
Service Lifecycle: Instancing Channel Q: How is my InstanceContext related to my Channel? A: InstanceMode InstanceContextService Provides connection between Channel and Service objectProvides connection between Channel and Service object Controls instance sharing and lifetimeControls instance sharing and lifetime
Service Lifecycle: Instancing ChannelInstanceContextService Instance Modes: PerCall (Default)PerCall (Default) PrivateSessionPrivateSession SharedSessionSharedSession SingletonSingleton ServiceBehavior(InstanceMode=Ins tanceMode.PerSession)] public class MyService : IMyService {…}
Service Lifecycle Instancing decision tree NeedState? Use Per Call – New context every call Use Singleton – One context for everyone Use Shared Session – Same as private session, but can share contexts within a group Use Private Session – One context per client StateScope? Shared? Yes Yes Grouped No No Global
Service Lifecycle: Service type ChannelInstanceContextService [OperationBehavior( ReleaseInstance=ReleaseInstance Mode.BeforeAndAfterCall)] public String Echo(String input) {…} By default, Service type lifetime is connected to InstanceContext Override this connection with ReleaseInstanceOverride this connection with ReleaseInstance BeforeCallBeforeCall AfterCallAfterCall BeforeAndAfterCal lBeforeAndAfterCal l
Community Resources At PDC For more information, go see COM423: WCF: Web Services Interoperability with Java/J2EE (Thurs 2:15pm) COM424: WCF: Extending the Channel Layer (Thurs 3:45pm) COM326: WCF: Web Services for XML Programmers (Thurs: 5:15pm) COM429: WCF: Extending the Service Model Layer (Fri: 10:30pm) Labs: COMHOL22, COMHOL28, COMHOL29 Ask The Experts table: General WCF (Thursday night) COM Track lounge After PDC If you missed this related session, watch it on the DVD COM416: WCF: Under the Hood of the WCF Channel Layer COM417: WCF: Under the Hood of the Service Model Layer MSDN dev center: Channel 9 tag:
© 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.