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

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

问题描述

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

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.

感谢您的建议,

蒂姆

按照 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:

你认为 .Net 远程处理依赖于紧耦合吗?

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

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