ChannelFactory.Open VS IClientChannel.Open

I am trying to better understand some of the internal actions of WCF. I look around a lot, but I could not find a clear explanation of what ChannelFactory.Open() is compared to IClientChannel.Open() . What is the purpose of opening a factory? If a channel is used for communication, what part does the factory execute in the process after the channel has been created and opened?

The question was asked here , among other questions, but it never answered directly.

EDIT:

After decompiling the source code, I found some of the specific reasons why Open needs to be called in ChannelFactory, which is described below.

I'm still having problems understanding why this work is done using the mechanisms provided by the ICommunicationObject when the factory is not actually reporting anything (as far as I know). Why not just handle these things when building or deleting an object?

I think that I am probably quite a few in the weeds that such an answer may not be publicly available. Thanks to those who weighed the original question.

+7
source share
3 answers

After decompiling a bunch of related classes in System.ServiceModel I was able to get a little more information.

It looks like the Open call breaks down the inheritance tree to CommunicationObject, where its Open method is called. All this seems to provide a set of diagnostic information and increases the number of events.

The ChannelFactory class uses open events to perform a number of tasks, including creating its internal factory channel:

 protected override void OnOpening() { base.OnOpening(); this.innerFactory = this.CreateFactory(); if (this.innerFactory == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("InnerChannelFactoryWasNotSet"))); } } 

As mentioned here, Close events are also used to perform actions such as closing all underlying channels (through the internal factory channel):

 protected override void OnClose(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); while (true) { IChannel channel; lock (base.ThisLock) { if (this.channelsList.Count == 0) { break; } channel = this.channelsList[0]; } channel.Close(timeoutHelper.RemainingTime()); } } 
+1
source

Open must be called in the factory, since it is an ICommunicationObject - before you use one of them, you need to open it. But in most cases, a factory opens automatically for you when you call things like CreateChannel, for example, so you rarely have to worry about opening the factory explicitly.

As for Close, it really depends on which binding the factory uses. In most cases, you’re right, the resource is mainly associated with the channel. But it is possible that a particular binding will multiplex several channels in the same basic connection, so closing the channel will simply remove the channel from the list to be multiplexed. Only when the factory closes does the base connection free.

+6
source

Opening a ChannelFactory or an internal channel simply changes the state of the object, when it is created, launched in the "Created" state, the object can be configured, but it cannot send or receive a message, in the "Open" state the communicationObject object but it is no longer configured

Thus, the goal of opening a factory is simply to choose a design, and it really does happen automatically when you create the first channel, it is not much under the hood, the factory is responsible for creating a channel that actually delivers messages from the transport layer and sends them to you.

Duct factories are responsible for creating the canals. channels created in channel factories are used to send messages. These channels are responsible for receiving the message from a higher level, performing any processing, and then sending the message to the layer below. The following figure shows this process.

http://msdn.microsoft.com/en-us/library/ms789050.aspx

Hope this helps

0
source

All Articles