Search code examples
c#wcfproxy

Generating wcf proxy vs ChannelFactory


Which one of this two ways of using wcf service is better? why?

  1. Generating proxy from Service Reference
  2. using ChannelFactory

ex.

ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>();
IMyContract proxy1 = factory.CreateChannel();
proxy1.MyMethod();

It is a bit boring to call wcf service like so

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?


Solution

  • Here is an MSDN post, that recomends not to use generated proxies in .Net 3 because it creates ChanelFactory each time, .Net 3.5 ChanelFactory is cached.

    But personally I prefer to use ChanelFactory myself, generated code is always a pain even after partials come out