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

时间:2011-10-03 09:51:36

标签: c# .net .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
  }
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

可能有一种方法使用*Services层次结构下的System.Runtime.Remoting个对象之一,如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; } }并覆盖它远程类。然后,您可以在if语句中使用该属性。

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

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);
    }
}