问题描述
是否有可能通过Remoting传递回调?我想要做的沿 myRemoteObject.PerformStuff的东西线(X => Console.WriteLine(X));
Is it possible to transmit a callback via remoting? I'd like to do something along the lines of myRemoteObject.PerformStuff( x => Console.WriteLine(x) );
如果没有,我将如何实现同样的功能?
If not, how would I implement a equivalent functionality?
编辑:我知道远程处理得到了WCF所取代,但我仍然有兴趣在此特定情况。 (出于好奇不过,怎么是这个问题在WCF?)
I'm aware Remoting got superseded by WCF, but I'm still interested in this specific case. (Out of curiosity though, how's the issue in WCF?)
推荐答案
Remoting提供事件支持。客户端code只需订阅这些受服务器类暴露的事件。 (有警告的大名单这里)。
Remoting provides support for events. The client code can simply subscribe to an event that's exposed by the server class. (There are a large list of caveats here).
此外,您还可以通过一个回调接口,远程类,提供您接口的实现是一个MarshalByRef对象。
Also, you can pass a callback interface to a remote class, providing your implementation of the interface is a MarshalByRef object.
interface IMyCallback
{
void OnSomethingHappened(string information);
}
class MyCallbackImplementation : MarshalByRefObject, IMyCallback
{
public void OnSomethingHappened(string information)
{
// ...
}
}
class MyServerImplementation : MarshalByRefObject
{
List<IMyCallback> _callbacks = new List<IMyCallback>();
public void RegisterCallback(IMyCallback _callback)
{
_callbacks.Add(_callback);
}
public void InvokeCalbacks(string information)
{
foreach(IMyCallback callback in _callbacks)
{
_callback.OnSomethingHappened(information);
}
}
}
这篇关于.NET Remoting的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!