如何返回一个能够在服务器上执行的对象?
来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议不要尝试“远程”对象。这是一个危险的想法。
真的,不要这样做。远程对象有点糟糕。在核心层面,网络是关于传输数据。从长远来看,让您的程序使用相同的模型将使您的生活变得更加轻松。
I recommend not trying to "remote" objects. It's a dangerous idea.
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.
WCF 确实是基于消息的,远程处理仍然有效......真正的问题是:为什么你不想基于消息工作?
WCF is indeed message based, Remoting still works.... the real question is: why don't you want to work message based?
如果您希望在 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.