Search code examples
c#wcfduplex

WCF Duplex Contract


Say I have WCF service contract like this

[ServiceContract(CallbackContract = typeof(ICallback1),
SessionMode = SessionMode.Required)]
public interface IService1
{
  // some methods
}

The service implementation has InstanceContextMode.Single set for InstanceContextMode and ICallback1 is something like

public interface ICallback1
{
    [OperationContract]
    void Report(int someValue);
}

Now on client side, I can have a class the implements ICallback1 like

class Callback1 : ICallback1
{
    public void Report(int someValue)
    {
        // alert client
    }
}

and I create client service reference like this

Service1Client serviceClient = new Service1Client(new InstanceContext(new CallBack1())); 

which works fine. Now the problem is that I have some clients that are not interested in the callback so I figured I do not require to implement the callback interface for such clients so I tried this

 Service1Client serviceClient = new Service1Client(null);

and

 Service1Client serviceClient = new Service1Client(new InstanceContext(null));

both reported that the parameter cannot be null. My question is, how can i create a service reference without passing a callback object if the client is not interested in the callback. The only requirement is that all clients should be talking to the same service but otherwise I can restructure the service however. Any thoughts ?

EDIT:

I have also tried SessionMode = SessionMode.Allowed for ServiceContract instead of SessionMode.Required but that didn't help either.


Solution

  • Workaround: Remove the CallbackContract from IService1. Create IDuplexService1 which inherits IService1 and contains the CallbackContract. Have Service1Client implement IDuplexService1. When instantiating the host, call ServiceHost.AddServiceEndpoint for both IService1 and IDuplexService1.