简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM