双工通信是否需要开放通道?
简洁版本: 当我在使用双工通信的客户端上使用 ChannelFactory 创建 Channel 时,我是否需要保持通道打开才能接收回调,或者我可以调用 ChannelFactory.Close() 吗?
长版: 我正在开发我的第一个 WCF 服务,并且创建了我自己的 ClientProxy 类,它实现了一些不同的服务并将其合并为一个。 我使用 ChannelFactory 来创建每个通道,我在网上的一般阅读表明我应该缓存 ChannelFactory,但我应该只在需要时打开和关闭实际通道。
因此,我调用 ChannelFactory.Open 打开一个通道并执行双工操作(一种单向操作,稍后调用回调)。 我是否应该在请求操作后通过调用 ChannelFactory.Close 来关闭此通道?如果这样做,我是否仍会收到回调?
基本测试似乎表明,如果我关闭连接,我将收到回调,但我只是想确定一下。 另外,这种缓存 ChannelFactory 的方法是否正确?
谢谢
Short Version:
When I've created a Channel using ChannelFactory on a client which uses duplex communication, do I need to keep the channel open in order to receive the callback or can I call ChannelFactory.Close()?
Long Version:
I'm developing my first WCF service and I've created my own ClientProxy Class, which implements and amalgamates a few different services into one. I use a ChannelFactory to create each channel, and my general reading on the net has indicated I should cache the ChannelFactory, but I should only open and close the actual channel when its needed.
So I call ChannelFactory.Open to open a channel and perform a duplex operation (a one-way operation which later calls a callback). Should I close this channel by calling ChannelFactory.Close after I've requested the operation, and if I do, will I still receive the callback?
Basic testing seems to indicate I will receive the callback if I close the connection however I just want to be sure. Also, is this method of caching the ChannelFactory correct?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您希望接收回调时,您应该保持客户端代理打开,完成后您应该关闭通道。
这里引用了 Juval Lowy 所著的《Programming WCF Services》一书(我建议您阅读有关回调的整章):
5.3.4。 回调连接管理
回调机制不提供任何类似于管理服务和回调端点之间连接的高级协议。 由开发人员提出一些应用程序级协议或一致的模式来管理连接的生命周期。 如前所述,只有当客户端通道仍然打开时,服务才能回调客户端,通常通过不关闭代理来完成。 保持代理打开还将防止回调对象被垃圾收集。 如果服务在回调端点上维护引用,并且客户端代理已关闭或客户端应用程序本身已消失,则当服务调用回调时,它将从服务通道获取 ObjectDisposeException。 因此,客户端最好在不再希望接收回调或客户端应用程序关闭时通知服务。 为此,您可以向服务契约添加显式的 Disconnect( ) 方法。 由于每个方法调用都带有回调引用,因此在 Disconnect() 方法中,服务可以从其内部存储中删除回调引用。
You should keep the client side proxy open while you wish to receive callbacks and when done you should close the channel.
Here's a quote from the great book Programming WCF Services by Juval Lowy (I suggest you to read the whole chapter about callbacks):
5.3.4. Callback Connection Management
The callback mechanism supplies nothing like a higher-level protocol for managing the connection between the service and the callback endpoint. It is up to the developer to come up with some application-level protocol or a consistent pattern for managing the life cycle of the connection. As mentioned previously, the service can only call back to the client if the client-side channel is still open, typically done by not closing the proxy. Keeping the proxy open will also prevent the callback object from being garbage-collected. If the service maintains a reference on a callback endpoint and the client-side proxy is closed or the client application itself is gone, when the service invokes the callback, it will get an ObjectDisposedException from the service channel. It is therefore preferable for the client to inform the service when it no longer wishes to receive callbacks or when the client application is shutting down. To that end, you can add an explicit Disconnect( ) method to the service contract. Since every method call carries with it the callback reference, in the Disconnect( ) method the service can remove the callback reference from its internal store.