ChannelFactory.Close VS IClientChannel.Close

前端 未结 4 429
灰色年华
灰色年华 2020-12-13 05:02

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

WSHttpBinding myBinding = new WSHttpBinding();
EndpointAddress myEndpoint = new         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 05:10

    The answer is already here, but it is spread over several comments and answers and not entirely clear, hence my answer.

    Should one close the ChannelFactory as well as the Channel?

    No. If you want to create multiple Channels from each ChannelFactory, you should dispose of the ChannelFactory, which will dispose all the Channels it created for you.

    If you want to create one channel for each (endpoint, binding) pair, you should use this static function: ChannelFactory.CreateChannel(binding, endpoint) (which avoids the issue as it does not create a second IDisposable), and you should dispose of the channel it returns.

    Disposing of both the channelfactory and any of the channels it created will raise an ObjectDisposed exception.

提交回复
热议问题