Monday, August 22, 2011

Adding custom EndPoint Behavior at client side ServiceReferences.ClientConfig

A custom Endpoint behavior which implements IEndpointBehavior can be added for many purposes. That depends upon your requirements and the nature of the application you are working on. But interesting thing is you cannot add the EndpointBehavior through config in Silverlight 4. But supports addition of custom Endpoint Behaviors through code .ie after creating the proxy you may add the behavior to the Endpoint.Behaviors collection of proxy.As you know, here proxy is a derived class of ClientBase<T> and this class has the property Endpoint.

Passing ClientBase<T> objects as method arguments
Next question comes how to make the code to add end point more generic in order to reuse the code for all the WCF service proxies.ie we need to write a method which accepts ClientBase<T>.This is little tricky at first look .But please don’t go for dynamic keyword or object where we have a typed way.Here is the small code snippet to use ClientBase<T> as method parameter type in C#.

private void CreateProxy()
{
TestServiceReference.TestServiceClient client = new TestServiceReference.TestServiceClient();
AddMyEndpointBahavior < TestServiceReference.ITestService>(client);
}
private void AddMyEndpointBahavior<T>(ClientBase<T> client) where T :class
{
client.Endpoint.Behaviors.Add(new MyEndpointBehavior());
}




For VB.Net version see my general coding blog where I posted this first since it is more related to generics.


No comments: