繁体   English   中英

如何知道在.NET Remoting中是在内部还是远程调用类方法?

[英]How to know whether a class method was called internally or remotely in .NET Remoting?

我有一个类,当通过.Net远程调用远程调用时需要表现不同。 在课堂上,如何确定是否属于这种情况?

class RemoteClass : MarshalByRefObject
{
 public void SomeMethod ()
 {
  if (ConditionWhatINeed) //If this method was called internally/remotely
  {
   //Do one stuff
  }
  else
  {
   //Do another suff
  }
}

您可能想要查看RemotingServices.IsObjectOutOfContext方法 它也有一个你可能会觉得有用的例子。 当然,因为您将在'this'上调用此方法服务器端,它永远不会被视为远程处理对象,但是如果您向方法添加一个参数,那么该参数将在本地上下文中,如果不进行远程处理和远程处理时的上下文(PS这是我帐户未经证实的假设)。 另一个有用的帮助器可能是RemotingServices.IsTransparentProxy方法

可能有一种方法使用System.Runtime.Remoting层次结构下的*Services对象之一,如mtijn所示。 但是,对象模型中存在深层问题。 对物体负有双重责任是不好的做法,难以维护和难以理解。 为什么不公开专用的“远程”对象; 以下示例演示了它:

class Program
{
    static void Main(string[] args)
    {
        InitializeRemoting();
        var remote = GetRemotingObject("localhost");
        var local = new LocalClass();

        remote.SomeMethod();
        local.SomeMethod();

        Console.ReadLine();
    }

    static void InitializeRemoting()
    {
        var c = new TcpServerChannel(9000);
        ChannelServices.RegisterChannel(c, false);
        WellKnownServiceTypeEntry entry = new WellKnownServiceTypeEntry
        (
            typeof(RemoteClass),
            "LocalClass", // Lie about the object name.
            WellKnownObjectMode.Singleton
        );

        RemotingConfiguration.RegisterWellKnownServiceType(entry);
    }

    static LocalClass GetRemotingObject(string serverName)
    {
        TcpClientChannel channel = new TcpClientChannel("tcp-client", new BinaryClientFormatterSinkProvider());
        ChannelServices.RegisterChannel(channel, false);

        return (LocalClass)Activator.GetObject
        (
            typeof(LocalClass), // Remoting will happily cast it to a type we have access to.
            string.Format("tcp://{0}:9000/LocalClass", serverName)
        );
    }
}

public class LocalClass : MarshalByRefObject
{
    public void SomeMethod()
    {
        OnSomeMethod();
    }

    protected virtual void OnSomeMethod()
    {
        // Method called locally.
        Console.WriteLine("Local!");
    }
}

// Note that we don't need to (and probably shouldn't) expose the remoting type publicly.
class RemoteClass : LocalClass
{
    protected override void OnSomeMethod()
    {
        // Method called remotely.
        Console.WriteLine("Remote!");
    }
}

// Output:
// Remote!
// Local!

编辑:直接回答你的问题,即使你想要实现的是不好的做法,重复我的代码,只需提供一个virtual bool IsLocal { get { return true; } } virtual bool IsLocal { get { return true; } }在本地类并覆盖其上的远程类。 然后,您可以在if语句中使用该属性。

编辑:如果您的服务器和您的客户端需要共享完全相同的类实例,您应该使用Facade模式 例如:

class CommonImplementation
{
    public static readonly CommonImplementation Instance = new CommonImplementation();
    private CommonImplementation() { }

    public void SomeMethod(string someArg, bool isServerCall)
    {
        if (isServerCall)
        {
            Console.WriteLine("Remote! {0}", someArg);
        }
        else
        {
            Console.WriteLine("Local! {0}", someArg);
        }
    }
}

// These two classes are the facade.
public class LocalClass : MarshalByRefObject
{
    public virtual void SomeMethod(string someArg)
    {
        CommonImplementation.Instance.SomeMethod(someArg, false);
    }
}
class RemoteClass : LocalClass
{
    public override void SomeMethod(string someArg)
    {
        CommonImplementation.Instance.SomeMethod(someArg, true);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM