Search code examples
c#wcfduplex

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);
}

My service:

[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);
    }

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:

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.

What could be causing all of it?


Solution

  • 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));
    

    and not like this:

    OperationContext.Current.GetCallbackChannel<IWebshopCallback> ().NewClientConnected ();