Friday, December 18, 2009

Best practices for creation of WCF proxy's in .Net

From past few days i was doing some findings about the best way of creating the service proxy's in WCF.

Here goes the result of that exercise..

1)

In this first case, We are creating the service proxy with the help of the ChannelFactory and we are closing that factory once we are done with the exercise,but it might so happen that during close call it might throw an exception,Afterwards that proxy state becomes faulted and it may be of no use(you cant make any call on that), hence to be on the safer side, by using a bool we are cleaning the instance if at all any exception happens. How cool right?


















2)

In this second case, using block will ensure that the proxy is Disposed, because the equivalent of the below code or using statement is




using (OrderServiceReference.OrderServiceClient proxy = new WindowsFormsApplication1.OrderServiceReference.OrderServiceClient())

{

int i = proxy.GetScore();

}





Equivalent code after compilation -

OrderServiceReference.OrderServiceClient proxy = default(OrderServiceReference.OrderServiceClient);

try
{

proxy = new WindowsFormsApplication1.OrderServiceReference.OrderServiceClient();

int i = proxy.GetScore();

}

finally
{

proxy.Dispose();

}



Thats all for now...

Regards,
-Vinayak


2 comments:

Rajavel JP said...

There is one more way of creating the client for WCF service. That is using the ClientBase.

public class OrderServiceClient : System.ServiceModel.ClientBase<IOrderService>, IOrderService
{
int Calculate(int A, int B)
{
return base.Channel.Calculate(A,B);
}
}

This will be helpful when we use bindings other than mex. Because, metadata will not be available at the client-side to create the proxy from the WCF service

Vinayaka Krishna Shenoy said...

Yes, Thats is the second case which i was mentioning about, where in i was using the proxy which was generated by using svcutil