wcf architecture and extensibility p oints
DESCRIPTION
CON 413. WCF Architecture and Extensibility P oints. Justin Smith Technical Evangelist http://blogs.msdn.com/justinjsmith. Agenda. Bindings Channel Managers Channels Messages XmlWriters and XmlReaders Message Encoders Dispatcher and Behaviors. WCF Architecture: Layers of Layers. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/1.jpg)
![Page 2: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/2.jpg)
WCF Architecture and Extensibility Points
CON 413
Justin SmithTechnical Evangelisthttp://blogs.msdn.com/justinjsmith
![Page 3: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/3.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 4: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/4.jpg)
Receiver
Channels
WCF Architecture: Layers of Layers
Sender
Proxy
Sender Application
Dispatcher
Receiver Application
Channel Layer
ServiceModel Layer
Channels
![Page 5: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/5.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 6: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/6.jpg)
What is a Binding?Abstraction of messaging functionality
Transport, security, transaction options, protocols
Creates a collection of BindingElement objects
Arrangement in collection is importantNetTcpBinding b = new NetTcpBinding();foreach (BindingElement el in b.CreateBindingElements()) { Console.WriteLine(el.GetType().Name);}
// outputsTransactionFlowBindingElement // protocolBinaryMessageEncodingBindingElement // encodingWindowsStreamSecurityBindingElement // securityTcpTransportBindingElement // transport
![Page 7: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/7.jpg)
What is a BindingElement?Create Channel Listeners & Channel Factories
listeners on receiver / factories on sendercollectively known as Channel Managers
Collection creates a stack (listeners/factories)
via BindingContext and CustomBinding typesBindingContext keeps a mutable list of BindingElement objects in CollectionCustomBinding is isomorphic
![Page 8: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/8.jpg)
Channel Factory StackBindingContext
CustomBinding
Walkthrough : Channel Factory
NetTcpBinding
TransactionFlowBindingElementBinaryMessageEncodingBindingElem
entWindowsStreamSecurityBindingElement
TcpTransportBindingElement
BuildChannelFactory<T>
Build
Inne
rCha
nnel
Fact
ory<
T>
TcpChannelFactory<T>
TransactionFlowChannelFactory<T>
Encoding and StreamSecurity absorbed by TcpChannelFactory via
BindingContext
![Page 9: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/9.jpg)
Walkthrough : Channel Factory public abstract class Binding : IDefaultCommunicationTimeouts { public virtual IChannelFactory<T> BuildChannelFactory<T> ( BindingParameterCollection parameters) { BindingContext context1 = new BindingContext(new CustomBinding(this), parameters); IChannelFactory<T> factory1 = context1.BuildInnerChannelFactory<T>(); return factory1; } // creates BindingContext, then calls BuildInnerChannelFactory}
public class BindingContext { public IChannelFactory<T> BuildInnerChannelFactory<T>() { return this.RemoveNextElement().BuildChannelFactory<T>(this); } // removes B.E. from list, then calls BuildChannelFactory on it}
public class MyBindingElement : BindingElement { public virtual IChannelFactory<T> BuildChannelFactory<T>(BindingContext c) { return new MyChannelFactory<T>(c); // ctor calls context’s } // returns the Channel Factory // BuildInnerChannelFactory}
![Page 10: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/10.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 11: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/11.jpg)
What is a Channel Factory?Create sending Channels
Stack of Channel Factories creates Channel stackChannel Factory stack is opaque
GetProperty<T> allows some query capabilitiesEach Channel Factory in stack:
Refers to the next Channel FactoryDelegates GetProperty<T> calls when unknown
EndpointAddress needed in channel stackUri for Via can be used as well
![Page 12: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/12.jpg)
What is a Channel Listener?Listens for incoming messages & Creates receiving channel stacksBottom-most Channel Listener listens on transport
Embrace “Accept” semantics similar to sockets“Accept” returns a
Channel Listener stacks are opaqueSymmetry with Channel Factory design
![Page 13: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/13.jpg)
Channel Stack
CreateChannel
Channel Factory Stack
Walkthrough : Channel Stack
TcpChannelFactory<T>
TransactionChannelFactory<T>
CreateChannel
TcpChannel<T>
TransactionChannel<T>
From a Channel Factory:
From a Channel Listener:
Channel Listener StackTcpChannelListener
TransactionChannelListener
AcceptChannelChannel StackTcpChannel<T>
TransactionChannel<T>AcceptChannel
message passing to channel stack not precise
![Page 14: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/14.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 15: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/15.jpg)
What is a Channel?Sends and/or receives Message objectsFor specific transport or protocol functionalityCommonly stacked w / other Channel objectsHave shape described by interfaces
MEP (datagram, request/reply, duplex)Composition of channel stack dictates messaging functionality
![Page 16: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/16.jpg)
Consider WS-RMSender Receiver
CreateSequenceCreateSequenceResponse (ID)
XSequence (ID, Msg #2)Sequence (ID, Msg #1)
Sequence (ID, Msg #3, LastMessage)
SequenceAck (ID, Msg #1, Msg #3)
Sequence (ID, Msg #2, AckRequested)
SequenceAck (ID, Msg #1 - Msg #3)
TerminateSequence (ID)
![Page 17: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/17.jpg)
Sending Application
Channel Stack
Receiving Application
Sequence (#2, LastMessage)
CreateSequence / CreateSequenceRespons
eSequence (#1)Ack (Msg #1-2)
Client
Walkthrough: WS-RM
HttpRequestChannel<T>
ReliableOutputSessionChannel TerminateSequence
![Page 18: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/18.jpg)
MEPs and The Channel LayerChannel Managers and Channels have shapeShape represents the supported MEP(s)
Datagram, Request/Reply, DuplexWCF represents shape via interfaces
Datagram: IInputChannel, IOutputChannelRequest / Reply: IRequestChannel, IReplyChannelDuplex: IDuplexChannelSessionful variants also exist
Interfaces have common type hierarchy
![Page 19: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/19.jpg)
Shapes and Interfacespublic interface IInputChannel : IChannel, ICommunicationObject { Message Receive(); // standard Begin / End methods also exist (and timeouts)...}
public interface IOutputChannel : IChannel, ICommunicationObject { void Send(Message msg); // standard Begin / End methods also exist (and timeouts)...}
public interface IDuplexChannel : IInputChannel, IOutputChannel, IChannel, ICommunicationObject { }
public interface IRequestChannel : IChannel, ICommunicationObject { Message Request(Message msg); // standard Begin / End methods also exist (and timeouts)...}
// IReplyChannel omitted for clarity
![Page 20: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/20.jpg)
ICommunicationObjectChannels and Channel Managers have a common state machine (also Faulted state)
Predictable state transitions
Created Opening Opened Closing Closed
Abort()
Abort()
Open() Close()
![Page 21: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/21.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcherBehaviors
![Page 22: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/22.jpg)
The Message TypeFundamental unit of communication
Channel layer interacts with Message objectsSeldom surfaces to the contract
CLR abstraction of a SOAP messageBody can be streamed, headers buffered
Can do non-SOAP formats (POX, JSON)public abstract class Message : IDisposable { public static Message CreateMessage(MessageVersion v, String a) {…} // lots of factory methods public void WriteMessage(XmlDictionaryWriter writer) {} // lots of write and read methods public abstract MessageHeaders Headers { get; }}
![Page 23: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/23.jpg)
Envelope / Addressing Versions
EnvelopeVersion identifies SOAP specs1.1, 1.2, none
AddressingVersion identifies WS-Addressing
Aug 2004, 1.0, noneMessageVersion wraps bothNone == no SOAP please
![Page 24: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/24.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 25: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/25.jpg)
XmlDictionaryWriterAn abstract type derived from XmlWriter
Wraps a System.IO.StreamCreated via factory methods
Specialized for binary, MTOM, text, JSONMemoryStream stream = new MemoryStream();XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false);dictionaryWriter.WriteElementString("localname", “http://namespace", "someValue");dictionaryWriter.Flush();
![Page 26: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/26.jpg)
XmlDictionaryReaderAn abstract type derived from XmlReader
Wraps a System.IO.Stream or bufferCreated via factory methods
Specialized for binary, MTOM, text, JSON
// Continued from previous slidestream.Position = 0;
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream);reader.Read();Console.WriteLine(reader.ReadOuterXml());
![Page 27: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/27.jpg)
Encoding Sample - Text
MemoryStream stream = new MemoryStream();using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false)) { writer.WriteStartDocument(); writer.WriteElementString(“SongName”, “urn:ContosoRockabilia”, “Aqualung”); writer.Flush();}// then read from Stream
XmlDictionaryWriter (Text-UTF8) wrote 97 bytes3C-3F-78-6D-6C-20-76-65-72-73-69-6F-6E-3D-22-31-2E-30-22-20-65-6E-63-6F-64-69-6E-67-3D-22-75-74-66-2D-38-22-3F-3E-3C-53-6F-6E-67-4E-61-6D-65-20-78-6D-6C-6E-73-3D-22-75-72-6E-3A-43-6F-6E-74-6F-73-6F-52-6F-63-6B-61-62-69-6C-69-61-22-3E-41-71-75-61-6C-75-6E-67-3C-2F-53-6F-6E-67-4E-61-6D-65-3E
data read from stream:<?xml version="1.0" encoding="utf-8"?><SongName xmlns="urn:ContosoRockabilia"> Aqualung</SongName>
![Page 28: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/28.jpg)
Encoding Sample - BinaryMemoryStream stream = new MemoryStream();using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(stream, null, null)) { writer.WriteStartDocument(); writer.WriteElementString(“SongName”, “urn:ContosoRockabilia”, “Aqualung”); writer.Flush();}// then read from Stream
XmlDictionaryWriter (Binary) wrote 43 bytes3F-08-53-6F-6E-67-4E-61-6D-65-04-15-75-72-6E-3A-43-6F-6E-74-6F-73-6F-52-6F-63-6B-61-62-69-6C-69-61-A1-08-41-71-75-61-6C-75-6E-67
data read from stream:N/A
![Page 29: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/29.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 30: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/30.jpg)
From Message to the Wire
Known as EncodingVia MessageEncoder and XmlWriter
MessageEncoder use Message.WriteMessageMessage.WriteMessage uses XmlWriter
public class MyEncoder : MessageEncoder { public override void WriteMessage(Message msg, Stream stream){ XmlWriter writer = XmlWriter.CreateWriter(stream); msg.WriteMessage(writer); writer.Flush(); } // other members omitted}
![Page 31: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/31.jpg)
From the Wire to Message
Known as DecodingDone via ReadMessage
uses one of the Message.CreateMessage overloadsCreateMessage overload accepts an XmlReader
public class MyEncoder : MessageEncoder { public override Message ReadMessage(Stream st, Int32 s, String ct){ XmlReader reader= XmlReader.Create(st); MessageVersion version = MessageVersion.Soap12WSAddressing10; return Message.CreateMessage(reader, s, version); } // other members omitted}
![Page 32: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/32.jpg)
Channel
Message Encoder
Channel
Message Encoder
Writ
eMes
sag
e
Walkthrough: Message-Wire-Message
Sender Receiver
Message
XmlWriterMessage
Read
Mes
sag
e XmlReaderBytesBytes
Message
![Page 33: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/33.jpg)
Channel Layer Service
demo
![Page 34: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/34.jpg)
AgendaBindingsChannel ManagersChannelsMessagesXmlWriters and XmlReadersMessage EncodersDispatcher and Behaviors
![Page 35: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/35.jpg)
About the DispatcherIn the abstract, the Dispatcher hides from the receiver the fact that the receiving application is receiving messagesThe Dispatcher is the realm of the contractMessages go in, Parameters come out, and a method is invoked
Receiver
Dispatcher
Receiving Application
Protocol / Shaping / Transport Channels
Parameters
![Page 36: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/36.jpg)
Extending the Dispatcher Overview
Message Inspection
Service BehaviorsOperation Selector
Message Formatting
Operation Invoker
Channel
Method1(…)
DispatchOperation
Method2(…)
DispatchOperation
DispatchRuntime
Parameter Inspection
Operation Behaviors
![Page 37: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/37.jpg)
Important Types for Extending the Dispatcher
To extend the Dispatcher, define a type that implements one of the followingIOperationInvoker
Invokes method on target objectIDispatchMessageFormatter
to control format of the MessageIDispatchOperationSelector
to select operation on target objectIDispatchMessageInspector
to inspect the MessageIParameterInspector
![Page 38: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/38.jpg)
Steps to Extend the Dispatcher1. Define a type that implements one of the
previous interfaces2. Define a type that implements one of the
Behavior interfaces and instantiates the type in Step # 1
3. Add the Behavior from Step #2 to the description via the appropriate place in the ServiceDescription
4. OPTIONAL – add configuration support5. OPTIONAL – add support for attribute
annotation
![Page 39: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/39.jpg)
IOperationInvokerEnables control over the invocation of a method on the receiving object
public interface IOperationInvoker { bool IsSynchronous { get; } object[] AllocateInputs(); object Invoke(object instance, object[] inpts, out object[] outputs); IAsyncResult InvokeBegin(object instance, object[] inpts, AsyncCallback cbck, object state); object InvokeEnd(object instance, out object[] outputs, IAsyncResult result);}
![Page 40: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/40.jpg)
IDispatchMessageFormatterEnables control over received Message deserialization and outbound Message serializationSerializeReply returns a serialized Message objectDeserializeRequest returns an Object deserialized from the MessageMessageVersion fed from the BindingSerializeReply is not invoked if operation is OneWay
public interface IDispatchMessageFormatter { void DeserializeRequest(Message msg, object[] parameters); Message SerializeReply(MessageVersion version, object[] parameters, object result);}
![Page 41: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/41.jpg)
IDispatchOperationSelectorEnables the selection of a particular DispatchOperationSelectOperation returns the name of the method associated with the DispatchOperation
Normally loaded from the ContractDescription
public interface IDispatchOperationSelector { String SelectOperation(ref Message message); }
![Page 42: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/42.jpg)
IDispatchMessageInspectorEnables a last view of the Message before it is sent into the Channel stack and at the point when the reply surfacesNotice that the Message is passed by reference
Be careful of Message state transitionsRequest and Reply messages can be correlated manuallyIf the operation is OneWay, BeforeReceiveReply not invoked
public interface IDispatchMessageInspector { Object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext);
void BeforeSendReply(ref Message reply, object correlationState);}
![Page 43: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/43.jpg)
IParameterInspectorEnables the inspection of parameters before the request is deserialized or the reply is serializedBeforeCall invoked before the Message is deserialized into parametersAfterCall invoked after the reply is sent
If the operation is a OneWay operation, then AfterCall is not invoked
BeforeCall and AfterCall can be correlated via an Object
public interface IParameterInspector { void AfterCall(String operationName, Object[] outputs, Object returnValue, Object correlationState);
Object BeforeCall(String operationName, Object[] inputs);}
![Page 44: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/44.jpg)
What Order Are Interceptors Invoked?
IDispatchOperationSelector
IDispatchMessageInspector
IOperationInvoker
IDispatchMessageFormatter
IDispatchMessageInspector
IDispatchMessageFormatter
IParameterInspector
Sender
IParameterInspector
IOperationInvoker
![Page 45: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/45.jpg)
Behaviors
demo
![Page 46: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/46.jpg)
Evaluation Forms
![Page 47: WCF Architecture and Extensibility P oints](https://reader035.vdocuments.net/reader035/viewer/2022081722/56815ed2550346895dcd63db/html5/thumbnails/47.jpg)
Questions?