3

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Paul Zhou
  • 193
  • 2
  • 12

1 Answers1

8

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)]
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Hi Ladislav, I have tried that. I also tried three solutions in this link: http://msdn.microsoft.com/en-us/magazine/cc163537.aspx#S4. However, it seems that it does not work for me. My scenario is that if I am calling back in a service contract method, it will deadlock. Please look at my Edit to review the code sample. – Paul Zhou Jul 17 '12 at 01:39
  • What is your client application? – Ladislav Mrnka Jul 17 '12 at 08:05
  • My client application just does nothing as a WPF application. something like this. public partial class MainWindow : Window, ICallback{ public void Callbackmethod(){}; } – Paul Zhou Jul 17 '12 at 09:55
  • So the problem can be your client because you are hosting callback in UI thread. – Ladislav Mrnka Jul 17 '12 at 09:59
  • So, maybe. In a button click event, I will call Service contract. And during the contract operation, it will invoke callback which implement in WPF client. Is it the problem? If so, how do I resolve it? – Paul Zhou Jul 18 '12 at 06:26
  • I just see your Edit. I tried add CallBackBehavior to my WPF Client which implements the callback contract. Yes, it is able to invoke calback now. However, in the callback contract operation, it will use some controls of WPF client, like "string text=this.textBox1.text". Then I will get InvalidOperationException: "The calling thread cannot access this object because a different thread owns it." – Paul Zhou Jul 18 '12 at 06:48
  • That is common issue - you cannot modify UI controls from different thread. There is [common pattern](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.checkaccess.aspx) to solve this - I recommend you to learn that pattern because it is a key feature if you want to make your UI responsive during long operations. The link shows how to invoke the modification asynchronously but synchronous version is possible as well. – Ladislav Mrnka Jul 18 '12 at 07:50
  • Yes, I have found that it is closed to threading safe issue of UI. It is similar to Invoke/BeginInvoke in WinForm application. Thank you very much for all your replies. – Paul Zhou Jul 18 '12 at 08:29
  • Thanks Ladislav Mrnka. You are a savior. I was facing a similar problem with my duplex service. I did what you said, and it is working. Thanks a lot. – Batrickparry Oct 18 '12 at 10:43