Search code examples
c#.netweb-serviceswcfwebservice-client

How To add WCF service reference for the duplex service?


I have a WCF service with callbacks. I want to create a client, BUT I want to do that automatically using "Add Service Reference" in Visual Studio. I'm able to discover the service and add it. I can update service reference as well. So it's there. The problem is with creating a client out of it.

If the service name is (in service references) "MyService", then creating a client for a normal (non-callback) service would be:

var myService = new MyServiceClient();
var data = myService.GetData();
myService.Close();

But How can I do that if the service implements Callback interface? I have to add context as a parameter for the Client, like this:

InstanceContext context = new InstanceContext(????);
var myService = new MyServiceClient(context);
var data = myService.GetData();
myService.Close();

but! I have to pass a client that implements callback interface into InstanceContext. Is there a way to quickly add client for the wcf service with callbacks?

Thanks for help!


Solution

  • You must create a callback handler:

    public class MyServiceCallbackHandler : IMyServiceCallback
    {
        public void Result(Data data)
        {
        }
    }
    

    and pass it to InstanceContext:

    InstanceContext context = new InstanceContext(new MyServiceCallbackHandler());
    var myService = new MyServiceClient(context);
    var data = myService.GetData();
    myService.Close();