Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

The WCF message formatter interfaces IClientMessageFormatter and IDispatchMessageFormatter let you intercept WCF messages on the client or service side at the point of serialization and deserialization. They're useful injection points as they give you access to the input or output parameter values of the service call, and to the incoming or outgoing WCF Message object. The formatters allow you to inspect or modify the message content or headers, using the call parameters to drive your logic.

Formatters need to be applied as operation behaviors, so there's no direct way to add them via binding configurations – typically they're added programmatically to the client channel, or to the service as attributes on the operation contract. But if your requirements are to add the same formatter to every operation and you want the flexibility of the configuration model, you can add formatters by piggybacking onto other behaviors which can be specified in configuration – IEndpointBehavior on the client and IServiceBehavior on the service:

(more detail on the data signature project in a future post).

Client Side

DataSignatureOperationBehavior.ApplyClientBehavior adds a DataSignatureClientFormatter to a single client operation:

public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)

{

DataSignatureClientFormatter formatter = new DataSignatureClientFormatter();

formatter.OrginalFormatter = clientOperation.Formatter;

clientOperation.Formatter = formatter;

}

Adding the formatter to one operation cannot be specified in client-side configuration. However the DataSignatureOperationBehavior can be added to every client operation in an endpoint using IEndpointBehavior.ApplyClientBehavior. DataSignatureEndpointBehavior adds the client formatter to every operation, by using the existing operation behavior:

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)

{

foreach (OperationDescription operation in endpoint.Contract.Operations)

{

operation.Behaviors.Add(new DataSignatureOperationBehavior());

}

}

The endpoint behavior is exposed in the client configuration mode by extending BehaviorExtensionElement, so the DataSignatureFormatter can be added to every operation in an endpoint by configuring the DataSignatureEndpointBehavior:

<client>

<endpoint address="http://localhost/x.y.z.svc"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICustomerService"

behaviorConfiguration="dataSignatureBehavior"

contract="CustomerService.ICustomerService" name="BasicHttpBinding_ICustomerService" />

</client>

<behaviors>

<endpointBehaviors>

<behavior name="dataSignatureBehavior">

<dataSignatureBehavior />

</behavior>

</endpointBehaviors>

</behaviors>

<extensions>

<behaviorExtensions>

<add

name="dataSignatureBehavior"

type="Sixeyed.OptimisticLockingSample.ServiceModel.Behaviors.DataSignatureEndpointBehavior…/>

</behaviorExtensions>

</extensions>

Service Side

The service stack is the parallel of the client side – DataSignatureDispatchFormatter is added via an operation behavior using DataSignatureOperationBehavior.ApplyDispatchBehavior:

public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)

{

DataSignatureDispatchFormatter formatter = new DataSignatureDispatchFormatter();

formatter.OrginalFormatter = dispatchOperation.Formatter;

dispatchOperation.Formatter = formatter;

}

Again this behavior is not available in the service configuration model, but it can be added to every operation in the service using IServiceBehavior.ApplyDispatchBehavior, as is done in DataSignatureServiceBehavior:

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)

{

foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)

{

foreach (OperationDescription operation in endpoint.Contract.Operations)

{

operation.Behaviors.Add(new DataSignatureOperationBehavior());

}

}

}

This behavior also extends BehaviorExtensionElement so it can be configured, adding the dispatch formatter will be added to every operation in the service:

<services>

<service behaviorConfiguration="dataSignatureBehavior"

name="Sixeyed.OptimisticLockingSample.Services.CustomerService">

<endpoint address="" binding="basicHttpBinding" contract="Sixeyed.OptimisticLockingSample.Services.ICustomerService"

bindingNamespace="http://Sixeyed.OptimisticLockingSample/2009"/>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name=" dataSignatureBehavior ">

<serviceMetadata httpGetEnabled="true"/>

<serviceDebug includeExceptionDetailInFaults="true"/>

<dataSignatureBehavior/>>

</behavior>

</serviceBehaviors>

</behaviors>

<extensions>

<behaviorExtensions>

<add

name="dataSignatureBehavior"

type="Sixeyed.OptimisticLockingSample.ServiceModel.Behaviors.DataSignatureServiceBehavior…/>

</behaviorExtensions>

</extensions>

Additionally, the DataSignatureOperationBehavior extends Attribute, so for finer-grained control the declarative approach is available using the same stack.

Posted on Saturday, April 4, 2009 10:15 PM Code Snippet , WCF | Back to top


Comments on this post: Applying WCF Message Formatters Using Configuration

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
I was having serious trouble with my behavior extensions before. Your code algorithm just lit a light bulb in me. Thanks.
Left by caravan porch awning on Oct 20, 2009 11:47 AM

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
This was a very well-written and enjoyable post to read.Some great information to be absorbed in this post.Thanks a lot for sharing.Keep blogging.
Left by application control58 on Apr 26, 2010 5:49 AM

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
WCF client is a client application creates to expose the service operations as method. Any application can host a WCF client, including an application that host a service.
Left by juicers on Oct 08, 2010 6:03 AM

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
This article shows me how to perform message validation using a schema in WCF. WCF is a framework for building and running service-oriented applications.
Left by platinum protection on Dec 12, 2010 8:45 AM

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
This is a good information. WCF client is a client application creates to expose the service operations as method. Thanks for sharing this post..
Left by neon lights on Feb 28, 2011 7:12 AM

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
Behavior extensions are difficult for me. Your code algorithm was helpful to me and my project. Thank you - good post.
Left by Neon Signs on Jun 15, 2011 3:59 PM

# re: Applying WCF Message Formatters Using Configuration
Requesting Gravatar...
BS, none of this works, where is DataSignatureOperationBehavior??
Left by Real Person on May 09, 2012 10:16 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net