为什么我的 Wcf Duplex 服务在首次调用该服务后会进入故障状态?
我想做的是拥有一个可供多个客户端注册的 wcf 双工服务。当客户端调用该服务时,其他客户端会收到该调用的通知。
现在,我有一个调用 wcf 双工服务的 Silverlight 应用程序。第一次通话时,一切正常,包括双工通信。在第二次调用该服务时,我得到:
The communication object, System.ServiceModel.Channels.ClientPollingDuplexSessionChannel, cannot be used for communication because it is in the Faulted state.
我的服务有两种不同的方法;注册客户端并重新分配所有者。如果我以任何顺序调用同一个方法两次或两个方法调用一次,我仍然会收到此错误。
在研究了这个主题之后,我认为这可能是一个超时问题。我检查了默认超时值是多少,是 10 分钟。我两次通话最多只用 2-3 分钟。为了确定起见,我还将不活动超时和接收超时增加到 4 小时,但我仍然遇到此问题。
接下来,我认为我的服务可能会生成异常,这会导致客户端进入故障状态。为了测试这一点,我将 try/catch 语句放入服务的 2 个方法中,并在调试模式下运行程序。我没有发现任何异常。我还尝试注释掉这两个方法中的所有代码(因此不会生成异常)并仅调用这两个空方法,但我仍然遇到异常。
通过阅读有关 SO 的其他相关文章,我决定尝试 WCF 跟踪。我在 web.config 中启用了它并将开关值设置为警告。我收到 2 个警告:
Description Faulted System.ServiceModel.Channels.ServicePollingDuplexSessionChannel
Description Faulted System.ServiceModel.Channels.ServiceChannel
这是我的服务在 .svc.cs 文件后面的代码中定义的方式:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, AutomaticSessionShutdown = false)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class NotificationService : BaseService, INotificationDuplexService
在我的服务合同中,我的方法都具有以下属性:
[OperationContract(AsyncPattern = true, IsOneWay = true,
Action = "http://www.abc.org/NotificationDuplex/INotificationDuplexService/MethodName")]
我的方法都是以异步方式实现的,具有 Begin 和 End 方法。
这是我调用方法的方法
base.InvokeAsync(this.onBeginRegisterClientDelegate, new object[] {
id}, this.onEndRegisterClientDelegate, this.onRegisterClientCompletedDelegate, userState);
有人知道为什么会发生这种情况,或者有任何线索告诉我如何继续尝试解决这个问题吗?
What I am trying to do is have a wcf duplex service that multiple clients register to. When a client calls the service, the other clients are notified of the call.
Right now, I have a Silverlight app that calls a wcf duplex service. On the first call everything is working correctly including duplex communication. On the second call to the service I am getting a :
The communication object, System.ServiceModel.Channels.ClientPollingDuplexSessionChannel, cannot be used for communication because it is in the Faulted state.
My service has 2 different methods; RegisterClient and ReassignOwner. If I call the same method twice or both method once in any order I am still getting this error.
After researching on the subject I thought it might be a timeout problem. I checked what the default timeout value was and it's 10 minutes. I do both calls within the span of 2-3 minutes tops. I have also increased the Inactivity timeout and the Receive timeout to 4 hours just to be sure and I am still getting this problem.
Next, I thought my service might be generating an exception which would cause the client to go in a faulted state. To test this, I have put try/catch statements in the 2 methods of my service and ran the program in debug mode. I did not catch any exception. I also tried commenting out all the code from those 2 methods (so no exception can be generated) and just calling those 2 empty methods and I still got the exception.
By reading other related post here on SO, I decided to try WCF tracing. I enabled it in my web.config and set the switch value to Warning. I get 2 warnings:
Description Faulted System.ServiceModel.Channels.ServicePollingDuplexSessionChannel
Description Faulted System.ServiceModel.Channels.ServiceChannel
Here is how my service is defined in the code behind .svc.cs file:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, AutomaticSessionShutdown = false)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class NotificationService : BaseService, INotificationDuplexService
In my service contract my methods both have the following attributes:
[OperationContract(AsyncPattern = true, IsOneWay = true,
Action = "http://www.abc.org/NotificationDuplex/INotificationDuplexService/MethodName")]
Both my methods are implemented in an async fashion with a Begin and an End method.
Here is how I invoke my methods
base.InvokeAsync(this.onBeginRegisterClientDelegate, new object[] {
id}, this.onEndRegisterClientDelegate, this.onRegisterClientCompletedDelegate, userState);
Anybody know why this is happening or have any clue to how I could continue trying to solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 ServiceContract 是否需要 CallbackContract?如果是这样,我建议您在此处阅读有关回调重入的内容 - 这是一个很好地描述了 WCF 如何在您使用回调时主动对您的通信通道进行故障排除以防止死锁,以及您应该采取哪些措施来防止死锁。
Does your ServiceContract require a CallbackContract? If so, I suggest that you read about callback reentrancy here - it's a good description of how WCF can proactively fault your communication channels to prevent a deadlock when you are using callbacks, and what you should do to prevent it.
我看到引用并自己实现的另一个解决方案是设置“心跳”。我将配置设置为 MultipleMessagesPerPoll,并且服务保持运行没有问题,因为我让客户端至少每 9 秒 ping 一次服务器。这似乎是一个神奇的数字——九秒后通道超时并进入故障状态。只需设置一个线程来 ping 服务(使用称为 ping 或其他方法的方法),服务就可以用“pong”响应客户端。
同意这个问题在Chrome或其他浏览器上似乎不是问题。
Another solution which I have seen referenced and I implemented myself is to set up a "heartbeat." I have my configuration set to MultipleMessagesPerPoll, and the service keeps running without a problem because I have the client pinging the server at least once every 9 seconds. That seemed to be a bit of a magic number -- after nine seconds the channel times out and goes into a faulted state. Just set up a thread to ping the service (using a method called ping or something) and the service can respond back to the client with a "pong."
Agree that this problem does not seem to be a problem on Chrome or other browsers.
最后我在类似的问题中找到了 awnser : Silverlight PollingDuplex InnerChannel failed with multipleMessagesPerPoll ( serverPollTimeout)
当您将 PollingDuplexHttpBinding 设置为 PollingDuplexMode.MultipleMessagesPerPoll 并在 IE 8 下运行应用程序时,似乎存在错误(其他浏览器不受影响)。
解决方案是将 PollingDuplexMode 改为 SingleMessagePerPoll,虽然性能较差,但在 IE8 中是通用的。
Finally I have found the awnser in a similar question : Silverlight PollingDuplex InnerChannel faulted with multipleMessagesPerPoll (serverPollTimeout)
It seems there is a bug when you set the PollingDuplexHttpBinding to PollingDuplexMode.MultipleMessagesPerPoll and you run your application under IE 8 (other browsers aren't affected).
The solution is to put the PollingDuplexMode to SingleMessagePerPoll which is less performant but focntionnal in IE8.