There is two way to create WCF proxy in client side
1. Generating proxy by adding Service Reference
2. By using ChannelFactory.CreateChannel method like this
ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>();
IMyContract proxy1 = factory.CreateChannel();
proxy1.MyMethod();
Which is best approach to get proxy?
And as mentioned in this article we should call proxy method like this for correct error handling
IMyContract proxy1 = null;
try
{
proxy1 = factory.CreateChannel();
proxy1.MyMethod();
((ICommunicationObject)proxy1).Close();
}
catch
{
((ICommunicationObject)proxy1).Abort();
}
Should we repeat this snippet for every proxy call? Or Is there generic way to create a wrapper class for closing and aborting proxies?
Is writing class like this ServiceExecution.Execute(proxy=>proxy.MyMethod());
which creates proxy, and closes or aborts it good way to do that?