简体   繁体   English

在执行合同操作期间调用回调

[英]invoke callback during the execution of a contract operation

I am programming a WCF service on Azure. 我正在Azure上编写WCF服务。

In my Service, I need to invoke callback during the execution of a contract operation. 在我的服务中,我需要在执行合同操作期间调用回调。 When I attempt to do that, an exception will throw and the client will locked. 当我尝试这样做时,将抛出异常并且客户端将被锁定。 I think it is caused by that the channel is opened for contract operation, invoking callback in current channel will lock the thread, am I right? 我认为这是因为通道打开合同操作,调用当前通道中的回调会锁定线程,对不对? I want to get solution for this scenario. 我想为这种情况获得解决方案。

here is the timeout exception message: 这是超时异常消息:

This request operation sent to net.tcp://127.255.0.0:8000/MytestWCFService did not receive a reply within the configured timeout (00:00:59.9889989). 发送到net.tcp://127.255.0.0:8000 / MytestWCFService的请求操作未在配置的超时(00:00:59.9889989)内收到回复。 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. 请考虑增加操作超时(通过将通道/代理强制转换为IContextChannel并设置OperationTimeout属性)并确保该服务能够连接到客户端。

Edit: code sample 编辑:代码示例

[ServiceContract(Namespace="testnamespace")]   
public interface ICallback   
{   
    [OperationContract(IsOneWay=true)]   
    void Callbackmethod();   
}  

Then I implement IContract in service side: 然后我在服务端实现IContract:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single,   
    ConcurrencyMode=ConcurrencyMode.Reentrant,   
    AddressFilterMode=AddressFilterMode.Any)]   
public class WCFService : IContract   
{      
   public int Add(int a, int b)   
   {   
       int result = a + b;  
       ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();   
       callback.Callbackmethod();   
       return result;   
   }   
}   

I am calling back in current channel, it is a duplex channel. 我正在回拨当前频道,它是一个双工频道。

If your operation and callback are two-ways you most probably have a deadlock issue. 如果您的操作和回调是双向的,那么您很可能遇到死锁问题。 Mark your service class with this attribute: 使用以下属性标记您的服务类:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MyService : IMyServiceContract { ... }

Edit: 编辑:

Also in your WPF application add this to implementation of the callback implementation: 同样在您的WPF应用程序中将此添加到回调实现的实现中:

[CallbackBehavior(UseSynchronizationContext = false)]

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

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