如何让 WCF 生成代理列表?
如何让 WCF 生成实际对象的代理列表或 IEnumerable
?我正在自托管应用程序中执行此操作。
这是我所拥有的:
public interface IRemoteControlGroup {
List<IRemoteControl> GetInstances();
}
public class RemoteControlGroupImpl : IRemoteControlGroup {
public List<IRemoteControl> GetInstances()
{
System.Console.Error.WriteLine("Called GetInstances()");
List<IRemoteControl> list = new List<IRemoteControl>();
// implementation detail: get this list of IRemoteControl objects
return list;
}
}
public interface IRemoteControl {
void Stop();
void Start();
void GetPID();
}
public class RemoteControlImpl : IRemoteControl {
// actual implementation
}
我希望 WCF 能够:
- 提供由
IRemoteControlGroup
上的合同定义的服务RemoteControlGroupImpl
。 - 当(在客户端)调用
IRemoteControlGroup.GetInstances()
时,给我一个List
,其中列表的元素是实现IRemoteControl
的代理code> (通过调用主机的实际IRemoteControl
对象)。
我不希望 WCF 通过线路推送实际的 RemoteControlImpl
对象;我只是希望它推送实现 IRemoteControl 的代理。 RemoteControlImpl 对象实际上包含本地系统的句柄(窗口句柄,因为我们的应用程序仅公开 GUI 界面),因此不可序列化。 GetInstance()
返回的元素数量可能会有所不同。
我找到了这篇文章,这听起来像我想要的。有点儿。但它没有告诉我如何在代码中执行此操作;只是在配置中。它也没有完全描述我想要的。服务的入口点提供一个代理;但我希望我的服务的入口点能够提供代理列表。
How do I get WCF to generate a list or IEnumerable
of proxies to the actual object? I'm doing this in a self-hosted application.
Here's what I have:
public interface IRemoteControlGroup {
List<IRemoteControl> GetInstances();
}
public class RemoteControlGroupImpl : IRemoteControlGroup {
public List<IRemoteControl> GetInstances()
{
System.Console.Error.WriteLine("Called GetInstances()");
List<IRemoteControl> list = new List<IRemoteControl>();
// implementation detail: get this list of IRemoteControl objects
return list;
}
}
public interface IRemoteControl {
void Stop();
void Start();
void GetPID();
}
public class RemoteControlImpl : IRemoteControl {
// actual implementation
}
I want WCF to:
- Offer a service,
RemoteControlGroupImpl
, defined by the contract onIRemoteControlGroup
. - Give me a
List<IRemoteControl>
whenIRemoteControlGroup.GetInstances()
is called (on the client), where elements of the list are proxies that implementIRemoteControl
(by calling the host's actualIRemoteControl
objects).
I don't want WCF to push actual RemoteControlImpl
objects through the wire; I just want it to push proxies that implement IRemoteControl
. RemoteControlImpl
objects actually contain handles to the local system (Window handles, because our apps only expose a GUI interface), and therefore, are not serializable. The number of elements returned by GetInstance()
can vary.
I found this article, which sounds like what I want. Kind of. But it doesn't tell me how to do this in code; just in the configuration. It also doesn't quite describe what I want. The entry point for the service delivers a proxy; but I want the entry point for my service to deliver a list of proxies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@John Saunders 指出的那样,您需要重新考虑您的方法。我可以想到一些可能有用的通用方法,具体取决于驱动“多个”实例的因素。
1) 如果它们是从应用程序外部驱动的(即可用 IRemoteControl 目标列表在运行时不会动态更改),那么您可以通过多个端点公开相同的合约。例如 http://localhost/remotecontrol.svc/instance1, http://localhost/remotecontrol.svc/instance2 等。不同端点的可用性可以通过 WSDL 公开。
2) 如果多个目标是动态的,那么最简单的方法是重新定义契约:
在内部,您的服务将维护可用 IRemoteControl 对象的字典,以 InstanceId 为键,并将传入操作简单地路由到目标实例。
As @John Saunders has pointed out you need to rethink your approach. I can think of a couple of general approaches that may be useful depending on what is driving your 'multiple' instances.
1) If they are driven from outside the application (i.e. the list of available IRemoteControl targets does not change dynamically at run time), then you could expose the same contract via multiple endpoints. E.g. http://localhost/remotecontrol.svc/instance1, http://localhost/remotecontrol.svc/instance2, etc. The availability of the different endpoints can be publicised via your WSDL.
2) If the multiple targets are dynamic then the simplest approach would be to redefine your contract:
Internally your service would maintain a dictionary of available IRemoteControl objects, keyed by InstanceId and simply route the incoming operation to the target instance.