如何为双工服务添加 WCF 服务引用?
How To add WCF service reference for the duplex service?
我有一个带回调的 WCF 服务。我想创建一个客户端,但我想使用 Visual Studio 中的 "Add Service Reference" 自动执行此操作。我能够发现该服务并添加它。我也可以更新服务参考。所以它就在那里。问题在于从中创建客户端。
如果服务名称是(在服务引用中)"MyService", 那么为普通(非回调)服务创建客户端将是:
var myService = new MyServiceClient();
var data = myService.GetData();
myService.Close();
但是,如果服务实现了回调接口,我该怎么做呢?
我必须添加上下文作为客户端的参数,如下所示:
InstanceContext context = new InstanceContext(????);
var myService = new MyServiceClient(context);
var data = myService.GetData();
myService.Close();
但是!我必须将实现回调接口的客户端传递给 InstanceContext
。有没有办法通过回调快速为 wcf 服务添加客户端?
感谢帮助!
您必须创建回调处理程序:
public class MyServiceCallbackHandler : IMyServiceCallback
{
public void Result(Data data)
{
}
}
并将其传递给 InstanceContext
:
InstanceContext context = new InstanceContext(new MyServiceCallbackHandler());
var myService = new MyServiceClient(context);
var data = myService.GetData();
myService.Close();
我有一个带回调的 WCF 服务。我想创建一个客户端,但我想使用 Visual Studio 中的 "Add Service Reference" 自动执行此操作。我能够发现该服务并添加它。我也可以更新服务参考。所以它就在那里。问题在于从中创建客户端。
如果服务名称是(在服务引用中)"MyService", 那么为普通(非回调)服务创建客户端将是:
var myService = new MyServiceClient();
var data = myService.GetData();
myService.Close();
但是,如果服务实现了回调接口,我该怎么做呢? 我必须添加上下文作为客户端的参数,如下所示:
InstanceContext context = new InstanceContext(????);
var myService = new MyServiceClient(context);
var data = myService.GetData();
myService.Close();
但是!我必须将实现回调接口的客户端传递给 InstanceContext
。有没有办法通过回调快速为 wcf 服务添加客户端?
感谢帮助!
您必须创建回调处理程序:
public class MyServiceCallbackHandler : IMyServiceCallback
{
public void Result(Data data)
{
}
}
并将其传递给 InstanceContext
:
InstanceContext context = new InstanceContext(new MyServiceCallbackHandler());
var myService = new MyServiceClient(context);
var data = myService.GetData();
myService.Close();