简体   繁体   English

双工WCF中的重新连接方案

[英]Re-connection scenario in duplex WCF

I have two application connected by a duplex WCF connection. 我有两个通过双工WCF连接连接的应用程序。 I works well, as long the connection is consistent. 只要连接保持一致,我就可以很好地工作。

I am checking now how to handle the re-connecting scenarios, when the connection is lost and it has to reconnect again. 现在,我正在检查如何处理重新连接的情况,当连接丢失并且必须再次重新连接时。 And I'm struggling to understand how work this out in WCF. 我正在努力了解如何在WCF中解决此问题。

As far as I knew, the IChannel is expendable, but the ChannelFactory is expensive. 据我所知, IChannel是消耗性的,但是ChannelFactory昂贵。 So I create one factory, and then the channel. 因此,我创建了一个工厂,然后创建了渠道。 Whenever I detect the Closed or Faulted events in the channel, I try to close the channel, deattach the event handlers and then create another channel. 每当我检测到通道中的“ Closed或“ Faulted事件时,我都会try关闭通道,取消附加事件处理程序,然后创建另一个通道。

But this approach is not working very well, because sometimes the DuplexChannelFactory<T>.CreateChannel gets faulted as well, and throws this exception: 但是这种方法不能很好地工作,因为有时DuplexChannelFactory<T>.CreateChannel出错,并引发以下异常:

System.ServiceModel.CommunicationObjectAbortedException occurred
  HResult=-2146233087
  Message=The communication object, System.ServiceModel.InstanceContext, cannot be used for communication because it has been Aborted.   

How is this possible that the factory itself gets faulted this way? 工厂本身如何通过这种方式出错?

What is the right approach to handle disconnections/reconnections in WCF? 在WCF中处理断开/重新连接的正确方法是什么?

I would not say it is the right way to do things but.... 我不会说这是正确的做法,但是...

My approach to handle disconnections/reconnections is to use a timer (on the client) to call a "ping" method on the service over the existing connection and recreate it if needed. 我处理断开连接/重新连接的方法是使用计时器(在客户端上)通过现有连接在服务上调用“ ping”方法,并在需要时重新创建它。 No idea if there is a better way, not that I didn't look. 不知道是否有更好的方法,不是我没有看过。

My problem was that the connection was being lost silently and the client ends up listening to a dead line and missing out on notifications. 我的问题是,连接无声地丢失了,客户端最终听了一条死线并错过了通知。

Trying to answer this part: 试图回答这一部分:

"How is this possible that the factory itself gets faulted this way?" “工厂本身如何通过这种方式出错?”

If you are IIS hosting your application your app pool could get recycled resulting in this behavior. 如果您是IIS托管应用程序,则您的应用程序池可能会被回收,从而导致此行为。 You check the logs to confirm if that is the root cause. 您检查日志以确认是否这是根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM