如何知道类方法是在.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; } },并在远程类上覆盖它。然后你可以在if语句中使用该属性。

Edit:如果您的服务器和您的客户端需要共享类的完全相同的实例,您应该使用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);
    }
}

相关内容

  • 没有找到相关文章

最新更新