[英]keeping a wcf callback channel open indefinitely / reconnecting from client if it faults
i'm currently trying to set up something like this: 我正在尝试设置这样的东西:
this works at first, but after a while stops -- i'm no longer able to pass calls to the client. 这首先工作,但一段时间后停止 - 我不再能够将呼叫传递给客户端。 i'm assuming it's because the connection has been dropped internally and i'm trying to work with a dead connection. 我假设它是因为连接已被内部删除,我正在尝试使用死连接。
that in mind, i've got the following questions: 记住,我有以下问题:
i can think of gimpy solutions, but I'm hoping someone here will tell me the RIGHT way. 我可以想到狡猾的解决方案,但我希望有人会告诉我正确的方法。
When you establish the connection from the client, you should set two timeout values in your tcp binding (the binding that you will pass to ClientBase<> or DuplexClientBase<>): 从客户端建立连接时,应在tcp绑定中设置两个超时值(将传递给ClientBase <>或DuplexClientBase <>的绑定):
NetTcpBinding binding = new NetTcpBinding();
binding.ReceiveTimeout = TimeSpan.FromHours(20f);
binding.ReliableSession.InactivityTimeout = TimeSpan.FromHours(20f);
My sample uses 20 hours for timeout, you can use whatever value makes sense for you. 我的示例使用20小时进行超时,您可以使用对您有意义的任何值。 Then WCF will attempt to keep your client and server connected for this period of time. 然后,WCF将尝试在这段时间内保持客户端和服务器的连接。 The default is relatively brief (perhaps 5 minutes?) and could explain why your connection is dropped. 默认是相对简短的(可能是5分钟?),可以解释为什么你的连接被删除。
Whenever there is a communication problem between the client and server (including WCF itself dropping the channel), WCF will raise a Faulted event in the client, which you can handle to do whatever you feel appropriate. 每当客户端和服务器之间存在通信问题时(包括WCF本身丢弃通道),WCF将在客户端引发一个Faulted事件,您可以处理该事件以做任何您认为合适的事情。 In my project, I cast my DuplexClientBase<> derived object to ICommunicationObject to get a hold of the Faulted event and forward it to an event called OnFaulted exposed in my class: 在我的项目中,我将我的DuplexClientBase <>派生对象转换为ICommunicationObject以获取Faulted事件并将其转发到我的类中暴露的名为OnFaulted的事件:
ICommunicationObject communicationObject = this as ICommunicationObject;
communicationObject.Faulted +=
new EventHandler((sender, e) => { OnFaulted(sender, e); });
In the above code snippet, this
is an instance of my WCF client type, which in my case is derived from DuplexClientBase<>. 在上面的代码片段中, this
是我的WCF客户端类型的一个实例,在我的例子中,它是从DuplexClientBase <>派生的。 What you do in this event is specific to your application. 您在此活动中所做的工作特定于您的申请。 In my case, the application is a non-critical UI, so if there is a WCF fault I simply display a message box to the end-user and shut down the app - it'd be a nice world if it were always this easy! 在我的情况下,应用程序是一个非关键的UI,所以如果有WCF错误我只是向最终用户显示一个消息框并关闭应用程序 - 如果它总是这么容易就会是一个美好的世界!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.