330 likes | 641 Views
WCF Communication Patterns: More than Request-Reply. Christian Weyer thinktecture Co-Founder & Solution Architect http://www.thinktecture.com/staff/christian christian.weyer@thinktecture.com. WCF Communication Patterns: More than Request-Reply. Christian Weyer thinktecture
E N D
WCF Communication Patterns:More than Request-Reply Christian Weyer thinktecture Co-Founder & Solution Architect http://www.thinktecture.com/staff/christian christian.weyer@thinktecture.com
WCF Communication Patterns:More than Request-Reply Christian Weyer thinktecture Co-Founder & Solution Architect http://www.thinktecture.com/staff/christian christian.weyer@thinktecture.com
What To Expect • ABC 101 • Internals 101 • MEPs 101 • Contracts and MEPs • Bindings and MEPs • Real-World Check
WCF: Bird’s Eye View Client Service Endpoint Endpoint Endpoint Endpoint
WCF: Address, Binding, Contract Client Service A B C A B C C B A A B C Address Where? Binding How? Contract What? Endpoint
WSDL Importer Service Model Layer Contract Contract WSDL Exporter Client Code parameters Service Type parameters Operation Invoker Dispatcher Runtime Parameter Inspector Parameter Inspector Client Runtime Message Formatter Message Formatter Operation Selector Message Inspector Message Inspector Channel Layer Channel Custom Channel Channel Custom Channel Channel Custom Channel Channel Custom Channel Transport Channel byte[] Transport Channel byte[] Encoder Encoder Custom Encoder Custom Encoder Custom Transport Custom Transport * Added by configuring the runtime with behaviors * Added by adding binding elements to the binding
Layered Extensibility Client Runtime Dispatch Runtime Protocol Channel(s) Transport Channel Protocol Channel(s) Transport Channel Service Model Extensibility (local only) Channel Extensibility (affects the wire)
Telephone or answering machine Synchronous vs. asynchronous execution Orthogonal to communication pattern Developers typically choose the way of least resistance Synchronous programming is what they are used to Threading, synchronization, callbacks et. al. are too cumbersome Real power is in decoupling systems Asynchronous communication in distributed solutions can solve a lot of issues – but can also introduce new ones
Message-based Communication Messages travel between communication parties Messages can be grouped into different message exchange patterns (MEPs) MEPs can be implemented synchronously or asynchronously WCF supports different MEPs One-way Request-reply Duplex WCF MEPs implemented by different kinds of channels
Channel Shapes IOutputChannel IInputChannel IRequestChannel IReplyChannel IDuplexChannel IDuplexChannel
Channel Shaping Possible mismatch: Physical transport Logical message exchange patterns Channel shapes implemented in transport bindings Express support for MEPs WCF ServiceModel analyzes contract Which channel shapes will be supported? ServiceChannelFactory.BuildChannelFactory and DispatcherBuilder.GetSupportedChannelTypes have the details
[Contracts]Request-Reply Most commonly used MEP It is easy to use and looks natural R/R can be implemented in WCF by several approaches Parameter list DataContract MessageContract Message Mind the ‚public face‘ of contracts, i.e. the metadata
[Contracts]One-Way O/W operations smell more feasible when decoupling is goal No return values, rather void Use IsOneWay property of OperationContract O/W operations can still block Not really fire-and-forget semantics
[Contracts]Duplex Having peer communication parties Blurring the line between client and server Use CallbackContract property of ServiceContract Operations in contracts can be one-way, but need no to be If using non-one-way beware of threading issues
[Contracts]Streaming Buffered communication can be very expensive with large data WCF offers streamed transfer Contract parameters must have certain shape Use Message, IXmlSerializable or System.IO.Stream or derivate as type Use MessageContract Headers will be buffered Body as System.IO.Stream will be streamed Only works with appropriate binding
[Bindings]Request-Reply Almost all out-of-the-box bindings support request-reply MSMQ-based bindings do not For all bindings and MEPs: Beware of default min/max values on binding elements for e.g. message sizes and buffers and timeouts Beware of possible throttles on service behavior
[Bindings]One-Way All bindings support one-way operations Use OneWayBindingElement for custom bindings to shape channel Performs a message-level shape change Can take a IDuplexSessionChannel or a IRequestChannel and expose it as a IOutputChannel, or conversely it can take a IDuplexSessionChannel or a IReplyChannel and expose it as a IInputChannel
[Bindings]Duplex Duplex out-of-the-box netTcp wsDualHttp netNamedPipe netPeerTcp Manual duplex session-less, e.g. <compositeDuplex /> <oneWay /> <binaryMessageEncoding /> <httpTransport />
[Bindings]Streaming Enable streaming on the baked-in bindings All standard bindings besides MSMQ-based Enable streaming on the transport binding element, e.g. <httpTransport transferMode=„Streamed“ /> Possible transferMode values Buffered Streamed StreamedRequest StreamedResponse Only transport security and no reliable sessions allowed
MSMQ transport MSMQ is different – always has been Duplex MEP over MSMQ-based binding can be achieved by e.g. doing it manually Leverage message headers Access headers through OperationContext in user code Access headers through message inspectors in ServiceModel extensions Use GUID to correlate messages
Real-World Check Common gotchas Nice features, but do I really need them? What about interop, e.g. with Java? Firewalls & NATs Deployment of MSMQ Support for mobile devices Not always is WCF the hammer, as not always everything is a nail
Sample Application: YAPOS ? Yet Another Purchase Order Scenario?No, don’t fear. Let’s bring some coolness into our dev life – talk about developer videos and media Every ‘good’ developer these days needs to deal with online resources and multimedia content MSDN.TV Channel 9 Dotnetpro.tv (German)
Sample Application Scenario Reviewer Media Service Web Server User New Media Service Message Queue Message Queue Upload Service Web Server
Resources Email Christian Weyer christian.weyer@thinktecture.com Weblog Christian Weyer http://blogs.thinktecture.com/cweyer thinktecture http://www.thinktecture.com