在另一个代理中包装RemotingProxy的__TransparentProxy引发RemotingException

时间:2018-07-30 14:36:24

标签: c# remoting realproxy

目标

我有几个接口和一些提供这些接口实现的dll。我想将实现加载到新的AppDomain中(以便以后再卸载dll),并在新的AppDomain中使实现无效,然后使用客户端(这里是默认的AppDomain)代理包装实际的实现对象。目标是一次创建这些ClientProxy实例,并在不将实现程序集加载到默认AppDomain中的任何时候更改其实际实现。

问题

在ClientProxy __TransparentProxy对象上调用一个方法(该方法将另一个ClientProxy作为参数)时,出现以下异常:

System.Runtime.Remoting.RemotingException: 'The argument type 'System.MarshalByRefObject' cannot be converted into parameter type 'IData'.'

具有内部异常:

InvalidCastException:Object must implement IConvertible.

传递直接从Server AppDomain获得的__TransparentProxy时,ClientProxy起作用。

设置

可克隆自:https://github.com/mailgerigk/remoting

接口:

public interface IData
{
    int Foo { get; set; }
}

public interface ILogic
{
    void Update(IData data);
}

_impl.dll中的接口Impl:

public class DataImpl : MarshalByRefObject, IData
{
    public int Foo { get; set; }
}

public class LogicImpl : MarshalByRefObject, ILogic
{
    public void Update(IData data)
    {
        data.Foo++;
    }
}

服务器端AssemblyLoader:

class AssemblyLoader : MarshalByRefObject
{
    public Assembly Assembly { get; private set; }

    public AssemblyLoader(string assemblyFile)
    {
        Assembly = Assembly.LoadFrom(assemblyFile);
    }

    public object CreateInstance(string typeName)
    {
        return Activator.CreateInstance(Assembly.GetType(typeName));
    }
}

ClientProxy:

class ClientProxy : RealProxy
{
    private RealProxy innerProxy;

    public ClientProxy(Type interfaceType, object proxyObject)
        : base(interfaceType)
    {
        SetInnerProxy(proxyObject);
    }

    public void SetInnerProxy(object proxyObject)
    {
        innerProxy = RemotingServices.GetRealProxy(proxyObject);
    }

    public override IMessage Invoke(IMessage msg)
    {
        return innerProxy.Invoke(msg);
    }
}

主要:

class Program
{
    static void Main(string[] args)
    {
        var app = AppDomain.CreateDomain("ImplDomain", null,
            AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.RelativeSearchPath,
            true);

        var assmblyLoader = app.CreateInstanceFromAndUnwrap(
            typeof(AssemblyLoader).Assembly.Location, typeof(AssemblyLoader).FullName,
            false, BindingFlags.CreateInstance, null,
            new object[]
            {
                "_impl.dll"
            },
            null, null) as AssemblyLoader;

        var dataImpl = assmblyLoader.CreateInstance("DataImpl") as IData;
        var logicImpl = assmblyLoader.CreateInstance("LogicImpl") as ILogic;

        logicImpl.Update(dataImpl); // Works
        Console.WriteLine(dataImpl.Foo); // prints 1

        var clientDataProxy = new ClientProxy(typeof(IData), dataImpl);
        var clientDataImpl = clientDataProxy.GetTransparentProxy() as IData;

        var clientLogicProxy = new ClientProxy(typeof(ILogic), logicImpl);
        var clientLogicImpl = clientLogicProxy.GetTransparentProxy() as ILogic;

        clientLogicImpl.Update(dataImpl); // Works
        Console.WriteLine(clientDataImpl.Foo); // prints 2

        clientLogicImpl.Update(clientDataImpl); // throws System.Runtime.Remoting.RemotingException
        Console.WriteLine(clientDataImpl.Foo);
    }
}

1 个答案:

答案 0 :(得分:1)

在不知道您的推理的情况下,从提供的示例代码中,ClientProxy类显得不必要,因为您可以直接使用RealProxy来获得相同的行为:

var clientDataProxy = RemotingServices.GetRealProxy(dataImpl);

话虽如此,您当然可以创建自己的具体RealProxy并使其按预期方式工作-您只是缺少从innerProxy获取TransparentProxy的替代:

class ClientProxy : RealProxy
{
    ... everything else ...

    public override object GetTransparentProxy() => innerProxy.GetTransparentProxy();

    public object GetOuterTransparentProxy() => base.GetTransparentProxy();
}

...
var clientDataProxy = new ClientProxy(typeof(IData), dataImpl);
var clientDataImpl = clientDataProxy.GetOuterTransparentProxy() as IData;

var clientLogicProxy = new ClientProxy(typeof(ILogic), logicImpl);
var clientLogicImpl = clientLogicProxy.GetOuterTransparentProxy() as ILogic;
...

按现状,您的ClientProxyTransparentProxy返回到外部对象。

编辑:基于注释中的解释,我添加了一个辅助GetOuterTransparentProxy方法,该方法返回外部基本实现。这仍将允许使用OOTB RealProxy.Invoke实现,同时暴露外部TransparentProxy以供重用。