Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating wcf proxy vs ChannelFactory

Tags:

c#

proxy

wcf

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?

like image 464
Arsen Mkrtchyan Avatar asked Aug 05 '10 11:08

Arsen Mkrtchyan


People also ask

What is a WCF proxy?

A WCF proxy is a CLR class that exposes the service contract. A Service proxy class has the service contract operations and some additional operations for managing the proxy life cycle and the connection to the service.

What is Channel Factory WCF?

A Channel Factory enables you to create a communication channel to the service without a proxy. A Channel Factory that creates and manages the various types of channels which are used by a client to send a message to various configured service endpoints.


2 Answers

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

like image 157
Arsen Mkrtchyan Avatar answered Oct 14 '22 20:10

Arsen Mkrtchyan


In first case when you use VS to add Service Reference it generates all the code for you including ServiceContrcats and DataContracts.

But when you use ChannelFactory you must have service contracts and etc on client side already.

like image 30
Incognito Avatar answered Oct 14 '22 22:10

Incognito