双工通信需要一个开放频道吗?

时间:2022-11-20 18:55:06

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()?

短版本:当我在使用双工通信的客户端上使用ChannelFactory创建频道时,我是否需要保持频道打开才能接收回调,或者我可以调用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.

长版本:我正在开发我的第一个WCF服务,并且我已经创建了自己的ClientProxy类,它实现并将一些不同的服务合并为一个。我使用ChannelFactory来创建每个通道,我在网上的一般读数表明我应该缓存ChannelFactory,但我只应该在需要时打开和关闭实际通道。

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?

所以我调用ChannelFactory.Open来打开一个通道并执行双工操作(一个单向操作,后来调用一个回调)。我应该在请求操作后通过调用ChannelFactory.Close关闭此通道,如果我这样做,我还会收到回调吗?

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?

基本测试似乎表明,如果我关闭连接,我将收到回调,但我只是想确定。此外,这种缓存ChannelFactory的方法是否正确?

Thanks

1 个解决方案

#1


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):

以下是Juval Lowy编写的WCF服务的好书(我建议你阅读关于回调的整章):

5.3.4. Callback Connection Management

5.3.4。回调连接管理

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.

回调机制不像用于管理服务和回调端点之间的连接的更高级协议。开发人员需要提供一些应用程序级协议或一致的模式来管理连接的生命周期。如前所述,如果客户端通道仍处于打开状态,则服务只能回调客户端,通常是通过不关闭代理来完成。保持代理打开也会阻止回调对象被垃圾回收。如果服务在回调端点上维护引用并且客户端代理关闭或客户端应用程序本身消失,则当服务调用回调时,它将从服务通道获得ObjectDisposedException。因此,当客户不再希望接收回叫或客户端应用程序正在关闭时,最好通知服务。为此,您可以向服务合同添加显式Disconnect()方法。由于每个方法调用都带有回调引用,因此在Disconnect()方法中,服务可以从其内部存储中删除回调引用。

#1


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):

以下是Juval Lowy编写的WCF服务的好书(我建议你阅读关于回调的整章):

5.3.4. Callback Connection Management

5.3.4。回调连接管理

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.

回调机制不像用于管理服务和回调端点之间的连接的更高级协议。开发人员需要提供一些应用程序级协议或一致的模式来管理连接的生命周期。如前所述,如果客户端通道仍处于打开状态,则服务只能回调客户端,通常是通过不关闭代理来完成。保持代理打开也会阻止回调对象被垃圾回收。如果服务在回调端点上维护引用并且客户端代理关闭或客户端应用程序本身消失,则当服务调用回调时,它将从服务通道获得ObjectDisposedException。因此,当客户不再希望接收回叫或客户端应用程序正在关闭时,最好通知服务。为此,您可以向服务合同添加显式Disconnect()方法。由于每个方法调用都带有回调引用,因此在Disconnect()方法中,服务可以从其内部存储中删除回调引用。