从 WCF 服务返回接口

发布于 2024-07-09 19:12:44 字数 716 浏览 8 评论 0原文

我有一些 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香草可樂 2024-07-16 19:12:44

抱歉,杰泽尔,我不明白。

是的,我可以在客户端上使用 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 to IFactory, since that singleton object has been "published" by the server through an URI on the ServiceHost.

But my IFoo instances on the server have not been associated with any ServiceHost; I just want to get them back by calling my IFactory proxy on the client, and let WCF do the call to the server IFactory, which would provide some IFoo, which would then be marshalled back to the client and wrapped into a dynamically generated proxy. I really just want to be able to write factory.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.

青朷 2024-07-16 19:12:44

ChannelFactory 类正是这样做的,在给定接口的运行时动态生成代理。

The ChannelFactory class does exactly this, generates a proxy dynamically at runtime given an interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文