wcfwcf-callbacksasynchronous-wcf-call

invoke callback during the execution of a contract operation


I am programming a WCF service on Azure.

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). 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.

Edit: code sample

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

Then I implement IContract in service side:

[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.


Solution

  • 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:

    [CallbackBehavior(UseSynchronizationContext = false)]