如何返回一个能够在服务器上执行的对象?

发布于 2024-08-25 00:59:47 字数 574 浏览 5 评论 0原文

来自 Java 背景的我的想法是这样的:

服务器向客户端提供一个对象。该对象应该能够在服务器上执行。

服务器:

private string _S = "A";

public interface IFoo { void Bar(); }

private class Foo : IFoo {
    void Bar() { _S = "B";}
}

public IFoo GetFoo() { return new Foo(); }

客户端:

IFoo foo = serverChannel.GetFoo();
foo.Bar();

远程处理是遗留的(每个人都一直指向 WCF)并且 WCF 基本上根本不支持这一点( WCF:有没有办法返回一个能够在服务器上执行的对象?),那么我应该如何实现这种行为呢?如果需要,可以使用第三方组件。

我搜索过SO但没有发现类似的问题。如果之前确实已经回答过这个问题,请告诉我,我会删除。

Coming from a Java background, this is the way I'm thinking:

The server provides an object to the client. This object should be able to execute on the server.

Server:

private string _S = "A";

public interface IFoo { void Bar(); }

private class Foo : IFoo {
    void Bar() { _S = "B";}
}

public IFoo GetFoo() { return new Foo(); }

Client:

IFoo foo = serverChannel.GetFoo();
foo.Bar();

Remoting is legacy (everyone keeps pointing to WCF instead) and WCF does not support this at all basically ( WCF: Is there a way to return an object that is able to execute on the server? ), so how should I implement this kind of behavior? Using 3rd party components is possible iff required.

I searched on SO but found no similar question. If this has indeed been answered before, just let me know and I'll delete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夏末 2024-09-01 00:59:47

我建议不要尝试“远程”对象。这是一个危险的想法。

  1. 您无法对远程状态进行本地控制。你永远不会。
  2. 试图推理什么是“真实”状态、什么不是“真实”状态很快就会变得非常复杂。
  3. 从网络角度来看,从消息角度思考可能会产生更“正确”的设计,即正确分配职责并且不会做出不适当假设的设计。
  4. 基于消息的网络应用程序几乎肯定会更加健壮。
  5. 基于远程处理的应用程序通常允许更快的初始开发,但从长远来看会导致大量额外时间处理边缘条件等。

真的,不要这样做。远程对象有点糟糕。在核心层面,网络是关于传输数据。从长远来看,让您的程序使用相同的模型将使您的生活变得更加轻松。

I recommend not trying to "remote" objects. It's a dangerous idea.

  1. You don't have local control over remote state. You never do.
  2. Trying to reason about what is the "true" state and what isn't gets very complicated very quickly.
  3. Thinking in terms of messages will likely result in a design which is more "correct" from a networking viewpoint - that is, a design which correctly allocates responsibilities and does not make inappropriate assumptions.
  4. A message-based network app will almost certainly be more robust.
  5. A remoting-based app will typically allow for faster initial development, but result in a ton of extra time in the long run dealing with edge conditions, etc.

Really, don't do it. Remote objects are just kind of bad. At a core level, networking is about transmitting data. Having your program work with the same model will make your life much, much easier in the long run.

萧瑟寒风 2024-09-01 00:59:47

WCF 确实是基于消息的,远程处理仍然有效......真正的问题是:为什么你不想基于消息工作?

WCF is indeed message based, Remoting still works.... the real question is: why don't you want to work message based?

柠檬心 2024-09-01 00:59:47

如果您希望在 WCF 中共享类型 - 就像您在远程处理中所描述的那样,在服务器和客户端上的公共程序集中共享(接口)声明 - 您可以使用 NetDataContractSerializer。它也帮助了其他人

它的使用是不鼓励的——就像远程处理一样——基于合同的消息传递现在似乎很流行。

我应该补充一点,通过正确的设计,即使使用.Net Remoting,您仍然会得到基于合同/消息的应用程序。您的共享接口将成为操作契约,而您的共享数据类定义将描述您传递的数据契约/消息。

If you want type sharing in WCF - like what you described and was in remoting, sharing (interface) declarations in common assemblies on the server and client - you can do it by using the NetDataContractSerializer. It helped others as well.

It's use is discouraged - just like remoting -, contract based messaging seems to be all the rage right now.

I should add that with a proper design you will still end up with a contract/message based application even with .Net Remoting. Your shared interfaces will become the operation contracts, while your shared data class definitions will describe the data contracts/messages you pass.

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