Tuesday, December 24, 2013

WCF Extensibility

Enter WCF extensibility points.
What is covered:
-          Service description vs Runtime
-          Behavior Interfaces
-          Runtime Extensibility
o   Interceptors
o   Formatters
o   Instancing
o   Error Handling
o   Some Others
-          Metadata Extensibility
-          Serialization Extensibility
-          WCF Channel  Programming Model
What is not  covered:
-          Some lesser-used interfaces
-          Security

Service description vs Runtime
-          Service (client) Description
o   All the things needed to start the service (client)
o   Ednpoints (Address/Binding/Contract)
o   Local Behaviors
-          Before “Open”
o   Description can be changes at will
o   Service not “running”
-          After “Open”
o   Runtime is initialized
o   Description is immutable
o   Listeners are started
o   The Service is started
-          WCF provides 4 interfaces to interact with the Service or Client descriptions
o   Service
o   Endpoint
o   Contract
o   operation
-          Each Interface has 4 methods which follow the same pattern.
-          public interface I[Service/Contract/Endpoint/Operation]Behavior
o   void validate(Description) : Prevent host or client from opening if it finds something invalid in the service description
o   void AddBindingParameters(Description, BindingParameterCollection): This gives an opportunity to the behavior to add more information to the Binding channels.
o   void ApplyDispatchBehavior(Description, Runtime):  This is the place where the behavior gets hooked up  to the runtime in the corresponding scope.
o   void ApplyClientBehavior(Description, Runtime) * {This is only client side}
-          Called during Open (Be it at the service side or the client side)
-          Ordering
o   Validate à AddBindingParameters à  Apply[Client/Dispatch]Behavior
o   Serviceà  Contract à Endpoint à Operation

-          Service Behavior
o   Service-wide operations
§  Metadata : Validation of the metadata exchange across the communication
§  Throttling
§  Debug
o   Public interface IServiceBehavior
§  Validate
§  AddBindingParameters
§  ApplyDispatchBehavior
o   Added via code, configuration, attributes
WCF Runtime   
WCF is a message stack with many message blocks from client calling the operation to the service call getting executed on the server.
We will see the most used blocks through which the messages pass.
·         WCF Runtime
·         Message Object
·         Message Inspectors
·         Parameter Inspector
·         Message Formatters
Client Proxy Call à Parameter Inspector à Fomatter à Message Inspector à WCF stack à Message Inspector à Formatter à Parameter Inspector à Service Operation

First question is what is a Message, what is the structure and Characteristics of the message.?
·         System.ServiceModel.Channel.Message à Abstract Class
·         SOAP/XML
·         Parts
o   Header – Buffered
o   Body
o   Properties : Dictionary not part of SOAP specification
·         Single-use : Once the message is used and read then it cannot be used any more. One can manipulate message headers at will.
·         Message Versions:  different versions of SOAP protocol which are envelopes like SOAP 1.1, 1.2 or none (message is not SOAP message but POX- Plain old XML)
·         XML
Message Inspectors :
Most used WCF extension from the WCF Runtime.
·         Usage: inspect, modify, replace messages
·         Scenarios: Logging, custom auth, tweaking messages, correlation between request and response
·         Server Side
o   MessageInspector is represented by the Interface: IDispatchMessageInspector
§  AfterReceiveRequest
§  BeforeSendReply
o   Dispatch behaviors are typically added as endpoint behavior as a part of ApplyDispatchBehavior to the DispatchRuntime object.
·         ClientSide
o   MessageInspector is represented by the Interface:  IClientMessageInspector
§  BeforeSendRequest
§  AfterReceiveReply
o   MessageInspectores are added as a part of service endpoint to the clientRuntime object.
o   Correlation object Used as correlation so that request can be set with something specific after the prior reply is received.
Parameter Inspector
Parameter inspectors are same as message inspectors but they are called on CLR objects not the message objects. They are easier to use as they do not require any knowledge about messages. Unlike Message inspector they are less powerful as they have less access to message itself.
·         Usage: Inspect and Modify operations inputs and outputs.
o   Return value cannot be modified
·         Scenarios: logging, parameter validation.
·         Same Interface for Client and Server : IParameterInspector
o   IParameterInspector
§  BeforeCall
§  AfterCall
·         Adding parameter inspectors often done in OperationBehavior
o   Server: DispatchOperation
o   Client: ClientOperation
·         Like MessageInspector ParameterInspector has a Correlation object is returned by BeforeCall and Passed to AfterCAll.

Message Formatters
Message formatters are the gateway between WCF messages and world of CLR objects.
·         Usage: convert between CLR objects and Message
·         Unlike Inspectors MessageFormatters are not optional but each operation has one and only one instance MessageFormatter.
·         Scenarios: supporting different formats, like passing some information as a part of httpHeaders.
·         Server Side
o   MessageFormatter is represented by the interface: IDispatchMessageFormatter
§  DeserializeRequest
§  SerializeReply
o   Message formatters can be accessed using the DispatchOperation in the Endpoint and operation behavior.
·         Client Side
o   MessageFormatter is represented by the interface: IClientMessageFormatter
§  DeserializeRequest
§  SerializeReply
o   Message formatters can be accessed using the ClientOperation in the Endpoint and operation behavior.
·         One difference between Inspectors and formatters is that it does not provide the Correlation object between request and reply.

No comments:

Post a Comment