Upload
dustin-harrell
View
216
Download
1
Tags:
Embed Size (px)
Citation preview
A Deep Dive into Best A Deep Dive into Best Practices with the Windows Practices with the Windows Communication FoundationCommunication Foundation
John JusticeJohn JusticeCOM415COM415Program Manager Program Manager Microsoft CorporationMicrosoft Corporation
What we’re covering What we’re covering today:today:10 – Choose a Binding that Fits Your 10 – Choose a Binding that Fits Your
NeedsNeeds
6 – Stay Flexible with Config6 – Stay Flexible with Config
5 – Send Faults, not Exceptions5 – Send Faults, not Exceptions
4 – Avoid UI Threading Conflicts4 – Avoid UI Threading Conflicts
3 – Use UDDI Internally3 – Use UDDI Internally
2 – Go Beyond XML Interoperability2 – Go Beyond XML Interoperability
1 – Understand the Service Lifecycle1 – Understand the Service Lifecycle
10: 10: Choose a Binding that Fits Your Choose a Binding that Fits Your NeedsNeeds
BasicHttpBindingBasicHttpBindingWsHttpBindingWsHttpBindingWsDualHttpBindingWsDualHttpBinding
Bindings: Bindings: Out of the BoxOut of the Box
NetTcpBindingNetTcpBindingNetNamedPipeBindingNetNamedPipeBindingNetMsmqBindingNetMsmqBindingNetPeerTcpBindingNetPeerTcpBinding
InteroperabInteroperablele
WCF to WCFWCF to WCF
Bindings: Bindings: Decision TreeDecision Tree
BasicHttpBindingBasicHttpBinding
WsHttpBindingWsHttpBinding
Interop?Interop?
What Level?What Level?
Duplex?Duplex?
WSDualHttpBindingWSDualHttpBinding
NetNamedPipeBindingNetNamedPipeBinding
NetMsmqBindingNetMsmqBinding
NetPeerTcpBindingNetPeerTcpBinding
NetTcpBindingNetTcpBinding
NoNo
BasiBasicc
NoNo
YesYes
Local?Local?
Need Queues?Need Queues?
Peer to Peer?Peer to Peer?
NoNo
NoNo
NoNo
YesYes
YesYes
YesYes
WSWS
YesYes
BindingsBindings
6: 6: Stay Flexible with ConfigStay Flexible with Config
Config: Config: Range of optionsRange of options
Deployment FlexibilityDeployment Flexibility
All CodeAll Code All ConfigAll Config
End-End-user user appsapps
EnterprisEnterprise appse apps
Config: Config: When you need to lock When you need to lock down…down…
Lock down only what you mustLock down only what you must
Check your Check your requirementrequirementss
Make sure Make sure your checks your checks are being are being executedexecuted
•Write a custom Write a custom ValidatorValidator
•Create your own Create your own BindingBinding•Require your Validator Require your Validator with with <commonBehaviors> in <commonBehaviors> in machine.configmachine.config
•Use a custom Use a custom ServiceHostServiceHost
Note: A malicious programmer can circumvent Note: A malicious programmer can circumvent any of these options in code.any of these options in code.
Stay Flexible with Config Stay Flexible with Config
5: 5: Send Faults, not ExceptionsSend Faults, not Exceptions
Send Faults, Not Send Faults, Not ExceptionsExceptions
Faults are at the contract levelFaults are at the contract level
Don’t shove implementation level Don’t shove implementation level exceptions into your Faultsexceptions into your Faults
When you get into trouble, send When you get into trouble, send business level faults, not exceptionsbusiness level faults, not exceptions
Indigo makes defining and sending Indigo makes defining and sending custom faults easycustom faults easy
Custom FaultsCustom Faults
4: 4: Avoid UI Threading ConflictsAvoid UI Threading Conflicts
Avoid UI Threading Avoid UI Threading ConflictsConflicts
Windows Forms and Windows Windows Forms and Windows Presentation Foundation apps use the Presentation Foundation apps use the main program thread for updatesmain program thread for updates
WCF services use the main program WCF services use the main program thread for processing requeststhread for processing requests
If you host a WCF service (or duplex If you host a WCF service (or duplex client) in a Windows Forms or WPF client) in a Windows Forms or WPF app, you’ll need to be careful app, you’ll need to be careful
Windows Forms ThreadingWindows Forms Threading
3: 3: Use UDDI InternallyUse UDDI Internally
Use UDDI InternallyUse UDDI Internally
UDDI isn’t just for public registriesUDDI isn’t just for public registries
When you need a layer of Web service When you need a layer of Web service indirection, think UDDIindirection, think UDDI
Development -> Test -> Staging -> Development -> Test -> Staging -> ProductionProduction
Choosing between non-clustered Choosing between non-clustered servicesservices
UDDIUDDI
2: 2: Go beyond XML InteroperabilityGo beyond XML Interoperability
Interop: Interop: Send Useful XMLSend Useful XML
Infrastructure interoperability:Infrastructure interoperability:== The plumbing can deliver your XML== The plumbing can deliver your XML
Being kind to your consumers:Being kind to your consumers:== There’s a good programming == There’s a good programming
experience on both sides of the wireexperience on both sides of the wire
Use a practical, platform neutral subset Use a practical, platform neutral subset of XML Schemaof XML Schema
Follow solid engineering practices when Follow solid engineering practices when designing your contractsdesigning your contracts
Interop:Interop: Contract Guidelines Contract Guidelines
Use our work: prototype with Use our work: prototype with DataContractDataContract
Use simple types that map well Use simple types that map well between .NET and Javabetween .NET and Java
DataSet is great for .NET to .NET, but DataSet is great for .NET to .NET, but isn’t supported well by other vendorsisn’t supported well by other vendors
Use typed, self-contained complex Use typed, self-contained complex typestypes
When writing XSD-first:When writing XSD-first:Avoid <xsd:AnyType />Avoid <xsd:AnyType />
Avoid <xsd:Choice />Avoid <xsd:Choice />
1: 1: Understand the Service LifecycleUnderstand the Service Lifecycle
Service Lifecycle: Service Lifecycle: Primary Primary playersplayers
ChannelChannel
•Lives for Lives for lifetime of lifetime of channel channel sessionsession
•Holds Holds security security informationinformation
•Holds Holds Reliable Reliable Session IDSession ID
InstanceContextInstanceContext
•Provides Provides connection connection between Channel between Channel and Service and Service objectobject
•Controls Controls instance sharing instance sharing and lifetimeand lifetime
ServiceService
•Your object Your object that that implements the implements the contractcontract
Service Lifecycle: Service Lifecycle: Channel Channel sessionsession
ChannelChannel
•Lifetime: Lives for Lifetime: Lives for the duration of the the duration of the channel sessionchannel session
•Channel lifetime Channel lifetime varies by transport varies by transport
InstanceContextInstanceContext ServiceService
What’s the session What’s the session lifetime of my channel?lifetime of my channel?
HTTP HTTP One Operation One OperationTCP TCP Connection ConnectionMSMQ MSMQ One Operation One Operation
Turn on Reliable Sessions Turn on Reliable Sessions to extend the session to extend the session lifetimelifetime
Service Lifecycle: Service Lifecycle: Channel Channel sessionsession
ChannelChannel
Turn on Reliable Turn on Reliable Sessions to Sessions to extend the extend the session lifetimesession lifetime
InstanceContextInstanceContext ServiceService
<bindings><bindings> <wsHttpBinding><wsHttpBinding>
<binding<binding name=“name=“……" >" > <reliableSession <reliableSession
enabled="true" />enabled="true" /></binding></binding>
</wsHttpBinding></wsHttpBinding></bindings></bindings>
Service Lifecycle: Service Lifecycle: Channel Channel sessionsession
ChannelChannel
Get finer-grained control Get finer-grained control with IsInitiating() and with IsInitiating() and IsTerminatingIsTerminating
InstanceContextInstanceContext ServiceService
// Close the Channel after calling// Close the Channel after calling[OperationContract([OperationContract(IsTerminating=IsTerminating=truetrue)])]public String Echo(String input);public String Echo(String input);
Service Lifecycle: Service Lifecycle: InstancingInstancing
ChannelChannel
Q: How is my Q: How is my InstanceContext InstanceContext related to my related to my Channel?Channel?
A: A: InstanceModeInstanceMode
InstanceContextInstanceContext ServiceService
•Provides Provides connection between connection between Channel and Channel and Service objectService object•Controls instance Controls instance sharing and lifetimesharing and lifetime
Service Lifecycle: Service Lifecycle: InstancingInstancing
ChannelChannel InstanceContextInstanceContext ServiceService
Instance Modes:Instance Modes:•PerCall PerCall (Default)(Default)•PrivateSessionPrivateSession•SharedSessionSharedSession•SingletonSingleton
ServiceBehavior(ServiceBehavior(InstanceModInstanceMode=InstanceMode.PerSessione=InstanceMode.PerSession)])]public class MyService : public class MyService : IMyServiceIMyService{{
……}}
Service LifecycleService LifecycleInstancing decision treeInstancing decision tree
NeedNeedState?State?
Use Use Per Call Per Call – New context – New context every callevery call
Use Use Singleton Singleton – One context for – One context for everyoneeveryone
Use Use Shared Session Shared Session – Same as – Same as private session, but can share private session, but can share contexts within a groupcontexts within a group
Use Use Private Session Private Session – One – One context per clientcontext per client
StateStateScope?Scope?
Shared?Shared?
YesYes
YesYes
GroupeGroupedd
NoNo
NoNo
GlobaGloball
Service Lifecycle: Service Lifecycle: Service Service typetype
ChannelChannel InstanceContextInstanceContext ServiceService
[OperationBehavior([OperationBehavior(ReleaseInstance=ReleaseInstReleaseInstance=ReleaseInstanceMode.BeforeAndAfterCalanceMode.BeforeAndAfterCalll)])]public String Echo(String public String Echo(String input)input){{
……}}
By default, Service By default, Service type lifetime is type lifetime is connected to connected to InstanceContextInstanceContext•Override this Override this connection with connection with ReleaseInstanceReleaseInstance
•BeforeCallBeforeCall•AfterCallAfterCall•BeforeAndAfterBeforeAndAfterCallCall
Community ResourcesCommunity ResourcesAt PDCAt PDC
For more information, go seeFor more information, go seeCOM423: WCF: Web Services Interoperability with Java/J2EE COM423: WCF: Web Services Interoperability with Java/J2EE (Thurs 2:15pm)(Thurs 2:15pm)COM424: WCF: Extending the Channel Layer (Thurs COM424: WCF: Extending the Channel Layer (Thurs 3:45pm)3:45pm)COM326: WCF: Web Services for XML Programmers (Thurs: COM326: WCF: Web Services for XML Programmers (Thurs: 5:15pm)5:15pm)COM429: WCF: Extending the Service Model Layer (Fri: COM429: WCF: Extending the Service Model Layer (Fri: 10:30pm)10:30pm)Labs: COMHOL22, COMHOL28, COMHOL29Labs: COMHOL22, COMHOL28, COMHOL29Ask The Experts table: General WCF (Thursday night)Ask The Experts table: General WCF (Thursday night)COM Track loungeCOM Track lounge
After PDCAfter PDCIf you missed this related session, watch it on the If you missed this related session, watch it on the DVDDVD
COM416: WCF: Under the Hood of the WCF Channel LayerCOM416: WCF: Under the Hood of the WCF Channel LayerCOM417: WCF: Under the Hood of the Service Model LayerCOM417: WCF: Under the Hood of the Service Model Layer
MSDN dev center: MSDN dev center: http://msdn.microsoft.com/webservices/http://msdn.microsoft.com/webservices/Channel 9 tag: Channel 9 tag: http://channel9.msdn.com/tags/Indigohttp://channel9.msdn.com/tags/Indigo
© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.