tags:

views:

38

answers:

2

In my current web project, we perform a ClientFactory.CreateChannel for every method call to a remote service.

Is this really necessary? What is the best practice?

A: 

This depends to some extent what your requirements are. Opening a channel is expensive, relatively speaking. Best practice is to have the class that is doing the remote calls implement IDisposable, it should make a call to ClientFactory.CreateChannel once, use the channels in all the method calls, and close the channel when the Dispose method is called. That said, if the time between the calls to methods that call to a remote service is long (longer then the default idle timeout on the channel which is 10 minutes) then doing a ClientFactory.CreateChannel isn't particularly harmful, but I would say it would still be better to go the IDisposable route and encapsulate use of the class with the 'using' keyword

Steve Ellinger
Actually: creating the channel is pretty light-weight - creating the ChannelFactory is what is quite "heavy" on performance...
marc_s
@marc_s: I think that depends, if the channel for example has security on it, the security negotiation takes place every time a socket is opened to the service, this can cause your method call to take significantly longer, imho.
Steve Ellinger
@marc_s did they not improve the creating of ChannelFactory? Or at least I'm sure I read that they improved creation after the first time. Update: http://blogs.msdn.com/b/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx
MattC
A: 

creating a new channel for each method call comes in bad practice "generally".

For Duplex WCF Service

creating a single channel and using it until there is no need to communicate with server anymore/ or that channel gets closed.

After creating the channel, before making any call to server, its recommended to check channel's state (Error, opening, closed).

Registering the channel closed/Error events is recommended to get to know immmediatley when it occurs. so you can take necessary actions or/and create the channel again with same object channel object reference.

For Normal WCF service

Create the proxy pattern, to create channel/ to re-use/ re create, error handling and disposing. set the appropiate inactivity timeout along with WCF client's proxy appropiate configuration which suits best with your solution.

Always Load test!!!!

Mazhar Karimi