Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF ChannelFactory State Property

What does it mean for a ChannelFactory to have a State property? I understand that a created channel can have connection based states. But am confused as to why the ChannelFactory also has such connection states. Does it too connect to the WCF service?

like image 310
Nicholas Avatar asked Jan 06 '10 07:01

Nicholas


1 Answers

A ChannelFactory object has a State because it is a CommunicationObject, and all CommunicationObjects in WCF have a State. Of course, that's just begging the question, and not really helpful.

The real question boils down to two parts

  1. Why does ChannelFactory derive from CommunicationObject
  2. What does its State actually means?

The second one is easier to answer so lets start there. The State of a ChannelFactory determines whether or not it can be used to create new client channels, and whether or not those client channels can still be used.

As with all CommunicationObjects in WCF, the State determines what operations you're permitted to do with the object. A channel factory really only has one operation: CreateChannel. If the factory is Open you can create channels; if it is Closed or Faulted, you cannot. The concrete (internal) channel factory implementations (say, and HttpChannelFactory) clean up any internal resources when they are Close()'d; this includes freeing resources that were created for security purposes, releasing handles to named pipes, etc.

In addition, when you Close() a channel factory, it loops through all of the channels and calls Close() on each of them, before transitioning into a Closed state itself. (There appears to be some common utility code (creating HTTP requests, etc.) that the channel factories implement on behalf of their channels, such that the channels could no longer function once the channel factory was shut down. This is why the channels are forced close at the same time.)

For all the gory details, go download the WCF Reference Source, but be prepared to lose a day or so :)

The bigger question, then, is why a ChannelFactory is a CommunicationObject at all? Here, I'm resorting to guessing, because as far as I can see the factory objects themselves never actually communicate to the remote system. However, they do perform a lot of setup and validation of their binding's parameters before they create a channel, which requires allocating the same kinds of resources that an actual network connection does. The named pipes channel factory, for example, creates and managed a connection pool for its channels; the HTTP and HTTPS channel factories validate identity information and authentication values. My guess is that the channel factories do this setup work once, so the channels can skip it; the CommunicationObject pattern simply provided a convenient way to manage the lifetime of a channel factory, since everything else in WCF is managed that way.

like image 176
Michael Edenfield Avatar answered Oct 08 '22 09:10

Michael Edenfield