从 WCF 服务返回接口
我有一些 .NET 远程处理代码,其中在某些服务器端类中实现的工厂方法返回具体对象的接口,也在同一台服务器上执行。 .NET 远程处理会自动创建代理,并允许我将接口传递给客户端,然后客户端可以直接调用它们。
接口示例:
public interface IFactory
{
IFoo GetFoo();
}
public interface IFoo
{
void DoSomething();
}
客户端代码示例:
...
IFactory factory = (IFactory) System.Activator.GetObject (typeof (IFactory), url);
...
IFoo foo = factory.GetFoo (); // the server returns an interface; we get a proxy to it
foo.DoSomething ();
...
这一切都很好。 但是,现在我正在尝试将我的代码迁移到 WCF。 我想知道是否有一种方法可以传递接口并让 WCF 在客户端上动态生成代理,就像原始的 .NET 远程处理一样。
我不想返回类实例,因为我不想公开真正的类。 并且序列化完整实例并在服务器和客户端之间来回发送它也不是一种选择。 我真的只是希望客户端通过接口指针/代理与服务器对象通信。
有任何想法吗?
I have some .NET remoting code where a factory method, implemented in some server side class, returns interfaces to concrete objects, also executing on the very same server. .NET remoting automagically creates proxies and allows me to pass the interfaces across to the client, which can then call them directly.
Example interfaces:
public interface IFactory
{
IFoo GetFoo();
}
public interface IFoo
{
void DoSomething();
}
Example client code:
...
IFactory factory = (IFactory) System.Activator.GetObject (typeof (IFactory), url);
...
IFoo foo = factory.GetFoo (); // the server returns an interface; we get a proxy to it
foo.DoSomething ();
...
This all works great. However, now I am trying to migrate my code to WCF. I wonder if there is a means to pass around interfaces and having WCF generate the proxies on the fly on the client, as does the original .NET remoting.
And I don't want to return class instances, since I don't want to expose real classes. And serializing the full instance and sending it back and forth between the server and the client is not an option either. I really just want the client to talk to the server object through an interface pointer/proxy.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用会话而不是客户端激活的对象 (MSDN )
Use Sessions instead of Client-Activated Objects (MSDN)
抱歉,杰泽尔,我不明白。
是的,我可以在客户端上使用
ChannelFactory
来创建IFactory
的代理,因为该单例对象已由服务器通过上的 URI“发布”服务主机
。但我在服务器上的
IFoo
实例尚未与任何ServiceHost
关联; 我只想通过在客户端调用我的IFactory
代理来取回它们,并让 WCF 调用服务器IFactory
,这将提供一些IFoo< /code>,然后将其编组回客户端并包装到动态生成的代理中。 我真的只是希望能够在我的客户端上编写
factory.GetFoo ();
...同时,Brian 向我指出了 MSDN 上我忽略的一个非常有趣的文档,它解释了如何通过使用会话和
EndPointAddress10
模仿 .NET Remoting 接口编组,并且...如您所写,ChannelFactory
来获取客户端代理。所以,现在我知道如何复制我的 .NET 远程处理代码,但为此付出了相对较高的成本。 WCF 涉及的代码复杂性比普通的旧 .NET 远程处理解决方案要高得多。
Sorry, jezell, I don't get it.
Yes, I can use
ChannelFactory
on the client to create a proxy toIFactory
, since that singleton object has been "published" by the server through an URI on theServiceHost
.But my
IFoo
instances on the server have not been associated with anyServiceHost
; I just want to get them back by calling myIFactory
proxy on the client, and let WCF do the call to the serverIFactory
, which would provide someIFoo
, which would then be marshalled back to the client and wrapped into a dynamically generated proxy. I really just want to be able to writefactory.GetFoo ();
on my client...In the meantime, Brian pointed me to a very interesting document I had overlooked on MSDN, which explains how to mimmick the .NET Remoting interface marshalling by using sessions and
EndPointAddress10
and ... as you wrote,ChannelFactory
to get the client side proxies.So, now I know how to replicate my .NET remoting code, but paying a relatively high cost for it. The code complexity involved with WCF is quite a bit higher than with the plain old .NET remoting solution.
ChannelFactory 类正是这样做的,在给定接口的运行时动态生成代理。
The ChannelFactory class does exactly this, generates a proxy dynamically at runtime given an interface.