ChannelFactory.Close VS IClientChannel.Close
考虑下面的代码,它是许多 ChannelFactory 示例的典型代码:
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();
请注意,当调用 proxy.Open() 时,通道的状态和 ChannelFactory 的状态都变为“Opened”。 当 proxy.Close() 被调用时,通道的状态变为“关闭”,但 ChannelFactory 的状态保持“打开”。
是否也应该关闭 ChannelFactory? 我似乎在很多例子中都没有看到这一点。 另外,如果可能的话,请解释打开通道与打开通道工厂之间的区别。
此外,我知道 IDisposable问题,因此对于这个问题,它可能可以被忽略,除非它对答案有直接影响。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现主要答案不准确,所以我在这里回复。
显然,微软已经把渠道、工厂和客户搞得彻底混乱。 文档也没有什么帮助,因为它们似乎只是为了掩盖混乱,所以我不得不诉诸测试。
由于与非缓存通道有关的性能问题,v3.5 来解决这些问题并添加了缓存,但这只会使问题变得更加复杂。
要点是,
ChannelFactory
中的通道实际上与使用ChannelFactory.CreateChannel()
创建通道时IClientChannel
使用的通道没有什么不同。 都是同一个锅。 不相信我吗? 尝试:实际上,内部都是相同的渠道。 我个人已经开始处理通道工厂而不是客户端通道,并且没有遇到任何问题。 我还尝试在循环中执行此操作,创建 100000 个客户端通道,并且仅关闭
ChannelFactory
。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 byIClientChannel
when you create a channel usingChannelFactory.CreateChannel()
. It is all the same pot. Believe me not? Try: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
.如您所知,ChannelFactory 根据配置创建客户端通道。 您可能希望从现有工厂创建多个客户端通道(到锁定的同一端点)。 如果您已完成使用工厂创建通道,则没有理由不关闭它。
但是,为什么你想保持它打开呢? 这是一篇有趣的有关 WCF 客户端的文章,内容如下:
您可能只想使用通道工厂创建一个新通道,而不是重用通道。 有关客户端架构的更多信息位于此处。
As you know, the ChannelFactory creates the client channel based on configuration. You may want to create multiple client channels from an existing factory (to the same endpoint as that is locked). If you're done using the factory to create channels, there is no reason not to close it.
But, why might you want to keep it open? Here's an interesting article on WCF clients that says:
Rather than reuse a channel, you might want to simply create a new one with the channel factory. More on the client architecture is here.
另一种选择是使用静态 CreateChannel 方法:
msdn.microsoft.com/en-us/library/aa344556.aspx
Another option is to use the static CreateChannel method:
msdn.microsoft.com/en-us/library/aa344556.aspx
答案已经在这里,但它分布在几个评论和答案中,并不完全清楚,因此我的答案。
不可以。如果您想从每个 ChannelFactory 创建多个 Channel,您应该处置 ChannelFactory,它将处置它为您创建的所有 Channel。
如果要为每一对(端点、绑定)创建一个通道,则应使用 此静态函数:
ChannelFactory.CreateChannel(绑定,端点)
(这避免了这个问题,因为它不会创建第二个 IDisposable),并且您应该处理它返回的通道。处置 ChannelFactory 及其创建的任何通道都会引发 ObjectDispose 异常。
The answer is already here, but it is spread over several comments and answers and not entirely clear, hence my answer.
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<ServiceType>.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.