如何在两个.NET AppDomain之间传递未知类型? [英] How to pass an unknown type between two .NET AppDomains?

查看:112
本文介绍了如何在两个.NET AppDomain之间传递未知类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET应用程序,其中单独的AppDomain中的程序集必须共享按值传递的序列化对象。

I have a .NET application in which assemblies in separate AppDomains must share serialized objects that are passed by value.

两个程序集都引用定义了基础的共享程序集服务器类的类,并且还定义了将在域之间传递的整个类型的基类:

Both assemblies reference a shared assembly that defines the base class for the server class and also defines the base class for the entiy type that will be passed between domains:

public abstract class ServerBase : MarshalByRefObject
{
    public abstract EntityBase GetEntity();
}

[Serializable]
public abstract class EntityBase
{
}

服务器程序集定义服务器类和实体类型的具体实现:

The server assembly defines the server class and a concrete implemetation of the entity type:

public class Server : ServerBase
{
    public override EntityBase GetEntity()
    {
        return new EntityItem();
    }
}

[Serializable]
public class EntityItem : EntityBase
{
}

客户端程序集创建 AppDomain ,服务器程序集将在其中托管并使用实例服务器类以请求实体类型的具体实例:

The client assembly creates the AppDomain in which the server assembly will be hosted and uses an instance of the server class to request a concrete instance of the entity type:

class Program
{
    static void Main()
    {
        var domain = AppDomain.CreateDomain("Server");

        var server = (ServerBase)Activator.CreateInstanceFrom(
            domain,
            @"..\..\..\Server\bin\Debug\Server.dll",
            "Server.Server").Unwrap();

        var entity = server.GetEntity();
    }
}

不幸的是,此方法以 SerializationException ,因为客户端程序集不直接知道要返回的具体类型。

Unfortnately, this approach fails with a SerializationException because the client assembly has no direct knowledge of the concrete type that is being returned.

我已了解到.NET远程支持使用二进制序列化时使用未知类型,但是我不确定这是否适用于我的设置或如何配置。

I have read that .NET remoting supports unknown types when using binary serialization, but I am not sure whether this applies to my setup or how to configure it.

或者,还有其他方法可以传递未知数假定客户端只需要通过其已知的基类接口访问它,就可以从服务器到客户端的具体类型。

Alternatively, is there any other way of passing an unknown concrete type from the server to the client, given that the client only needs to access it via its known base class interface.

感谢您的建议,

Tim

编辑:

根据Hans的要求,这是异常消息和堆栈跟踪。

As requested by Hans, here is the exception message and stack trace.

SerializationException
Type is not resolved for member 'Server.EntityItem,Server, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null'.

at Interop.ServerBase.GetEntity()
at Client.Program.Main() in C:\Users\Tim\Visual Studio .Net\Solutions\MEF Testbed\Client\Program.cs:line 12
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


推荐答案

不久前我问了一个相关问题:

I asked a related question a while back:

Would you say .Net remoting relies on tight coupling?

这篇关于如何在两个.NET AppDomain之间传递未知类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆