Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChannelFactory.Close VS IClientChannel.Close

Consider the following code which is typcial of many ChannelFactory examples:

WSHttpBinding myBinding = new WSHttpBinding(); EndpointAddress myEndpoint = new EndpointAddress(    ConfigurationSettings.AppSettings["HelloWorldServiceURL"]);    ChannelFactory<IHelloWorldService> myChannelFactory =     new ChannelFactory<IHelloWorldService>(myBinding, myEndpoint);  IHelloWorldService proxy = myChannelFactory.CreateChannel(); ((IClientChannel)proxy).Open(); HelloWorldDataContract dc = proxy.SayHello(); ((IClientChannel)proxy).Close(); 

Note that when proxy.Open() is called, both the the channel's state and the ChannelFactory's state become "Opened". When proxy.Close() is called, the channel's state becomes "closed", but the ChannelFactory's state remains "Opened".

Should one be closing the ChannelFactory as well? I don't seem to see this in many examples. Also, if possible please explain the difference between having a channel open vs having a channel factory open.

Additionally, I am aware of the IDisposable issue, so it probably can be ignored for the sake of this question unless it has direct impact on the answer.

like image 700
Daniel Auger Avatar asked May 14 '09 22:05

Daniel Auger


People also ask

What is ChannelFactory in WCF?

ChannelFactory<TChannel> Class (System.ServiceModel)A factory that creates channels of different types that are used by clients to send messages to variously configured service endpoints.

How does Channel factory work?

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.


1 Answers

I found the main answer inaccurate so I am responding here.

Obviously Microsoft has made an absolute mess out of Channles and Factories and Clients. Documentation is not also helpful since they seem to be there just to cover up the mess so I had to resort to testing.

With the performance issues regarding non-cached Channels, implementation changed in v3.5 to address these and added caching but that only complicated the issue.

The point is channel in ChannelFactory is in fact not different from the channel used by IClientChannel when you create a channel using ChannelFactory.CreateChannel(). It is all the same pot. Believe me not? Try:

ChannelFactory<IService> factory = new ChannelFactory<IService>(); // ... IService service = factory.CreateChannel(); factory.Close(); service.DoIt() // Throws object disposed exception 

So really, internally it is all the same channel. I personally have started disposing the channel factories and not client channels and have not encountered any issue. I have also tried doing this in a loop with creation of 100000 client channels, and only closing ChannelFactory.

like image 169
Aliostad Avatar answered Sep 21 '22 11:09

Aliostad