再次更新赏金,因为我真的需要知道如何让它发挥作用,或者明确回答为什么它不会。
I've added an alternative explanation of the problem here.
让双向 (IsOneWay = false) WCF 客户端-服务器在 .Net 3/3.5 中工作真是费尽心思。
客户端成功注册服务后,服务的周期性 Announcement() 回调到注册的客户端。现在是客户端或服务器挂起,直到服务器的 SendTimeout(调整为 2 秒)过去。那么服务器端出现超时异常如下。只有这样,客户端用户代码才会立即接收方法调用并尝试返回一个值。到那时,客户端的套接字被中止并且 WCF 的东西失败了。
在我看来,客户端上的某些东西正在将其本地 WCF 队列从处理中挂起,直到套接字超时,但还不足以取消本地方法调用。但如果可以相信下面的异常,服务器正试图向 http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous 发送操作。 (不合适!)并且正在超时。也许该 URI 只是远程连接客户端的“名称”,因为 WCF 知道出于错误消息的目的引用它,它似乎只是意味着它无法加载 URI。我不知道是服务器先失败还是客户端先失败。
我已经尝试添加 WCF 跟踪,但我没有获得更多信息。
Similar sample code is here ,但它一定是太多了,无法消化。我已经尝试过该代码的变体。
TimeoutException 'This request operation sent to http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous did not receive a reply within the configured timeout (00:00:00). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client.'
Server stack trace:
at System.ServiceModel.Dispatcher.DuplexChannelBinder.SyncDuplexRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
最佳答案
首先给自己弄一份Programming WCF Services ,如果你还没有的话。
如果客户端是WinForm或者WPF,需要使用[CallbackBehavior(UseSynchronizationContext = false)]
否则客户端不会处理传入的消息,直到 UI 线程进入消息循环。
首先,WCF 中的“双工” channel 并不是真正的双工! 来自的消息
因为消息仅在单个 WCF channel 上按顺序分派(dispatch)。双工 WCF channel 不会为您提供两个传入消息队列。从“TwoWay”调用返回的结果与 WCF 堆栈的这一级别的“调用”相同。 一旦你明白这一点,很多问题就会变得更清楚明白。
如果客户端是 WinForm 或 WPF,可能需要使用
[CallbackBehavior(UseSynchronizationContext = false)]
否则客户端不会处理传入的消息,直到 UI 线程进入消息循环。我发现了一些有助于避免死锁的规则。 (看看我的 WCF 问题,看看我的痛苦!)
The sever must never call out to a client on the same connection as a call from the same client is in process on.
和/或
The client must never call back to the server on the same connection as is used for the “callbacks” while processing a call-back.
下次我想我将只使用两个契约(Contract)(以及因此的 TCP 连接)一个用于回调,另一个用于所有客户端->服务器请求。或者使用我自己的投票系统,因为这让我非常痛苦。
抱歉我今天没有时间写一个例子。无论如何,大多数示例都适用于示例试图做的事情,但在现实生活中由于某种原因与您的应用程序有关。
我所知道的关于 WCF 示例的最佳网站是 Juval Lowy’s web site .
您还可以找到 questions I asked about WCF on Stack Overflow有用,因为我遇到了和你一样的问题。
花一两天时间阅读 Stack Overflow 上的所有 WCF 问题和答案,可以很好地了解要避免的问题。
关于wcf - 求 WCF 双工 "TwoWay"订阅+回调示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472679/