实现双工时 WCF 中的超时异常 [英] TimeOut exception in WCF while implementing duplex
问题描述
我的服务合同和回调合同如下所示:
My service contract and callback contracts look like this:
[ServiceContract(CallbackContract = typeof(IWebshopCallback))]
interface IWebshop
{
[OperationContract]
string GetWebshopName ();
[OperationContract]
string GetProductInfo (Item i);
[OperationContract]
List<Item> GetProductList ();
[OperationContract]
bool BuyProduct (string i);
[OperationContract]
void ConnectNewClient ();
}
[ServiceContract]
interface IWebshopCallback
{
[OperationContract]
void NewClientConnected (int totalNrOfConnectedClients);
}
我的服务:
[ServiceBehavior (InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
class WebshopService : IWebshop
{
...
}
在服务内部,我有一个方法可以调用客户端的另一个方法:
Inside of the service I have a method which calls the other method on a client's side:
public void ConnectNewClient ()
{
totalNumber++;
OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected (totalNumber);
}
在客户端,我有一个派生自 IWebshopCallback 的表单,并有一个方法 NewClientConnected(int a).
And on the client's side I have a form which derives from IWebshopCallback and has a method NewClientConnected(int a).
问题是当我尝试运行我的代码时遇到了这个异常:
The problem is that when I try to run my code I run into this exception:
这个发送到 http://localhost:4000/IWebshopContract 的请求操作在配置超时 (00:00:59.9989895).分配给此操作的时间可能是较长超时的一部分.
This request operation sent to http://localhost:4000/IWebshopContract did not receive a reply within the configured timeout (00:00:59.9989895). The time allotted to this operation may have been a portion of a longer timeout.
不过,更奇怪的是,如果我继续运行我的应用程序,我会发现此功能有效.
What's more strange, though, is that if I continue the work of my app I see that this function worked.
是什么导致了这一切?
推荐答案
在服务器端,当你调用一个函数时你需要使用 Task.Run(),所以你应该像这样:
On the server side when you call on a function you need to use Task.Run(), so you should have it like this:
var callback = OperationContext.Current.GetCallbackChannel<IWebshopCallback> ();
Task.Run (() => callback.NewClientConnected(totalNumber));
而不是这样:
OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected ();
这篇关于实现双工时 WCF 中的超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!