如何在两个.NET AppDomains之间传递未知类型?

时间:2010-11-15 15:21:26

标签: .net remoting appdomain

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

两个程序集都引用一个共享程序集,该程序集定义服务器类的基类,并定义将在域之间传递的entiy类型的基类:

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

[Serializable]
public abstract class EntityBase
{
}

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

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

[Serializable]
public class EntityItem : EntityBase
{
}

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

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,因为客户端程序集并不直接了解要返回的具体类型。

我已经读过使用二进制序列化时.NET Remoting支持未知类型,但我不确定这是否适用于我的设置或如何配置它。

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

感谢您的建议,

编辑:

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

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()

3 个答案:

答案 0 :(得分:2)

这失败了,因为CLR没有希望能够找到程序集,你把它放在一个不可靠的位置。通过添加对程序集的引用并将其Copy Local属性设置为True来简单地解决此问题,以便将server.dll复制到您的构建目录中。如果你想将它保持在原来的位置,那么你必须实现AppDomain.AssemblyResolve以帮助CLR找到它。

答案 1 :(得分:1)

我回答了一个相关的问题:

Would you say .Net remoting relies on tight coupling?

答案 2 :(得分:0)

我认为我有一个解决方案,感谢当前的帖子,这个及其接受的答案:AppDomain.Load() fails with FileNotFoundException

首先,我认为您应该使用接口代替基类作为处理程序。接口应该在基类上声明,然后你只能使用它。

解决方案:在共享程序集中创建一个具体类型,它继承自MarshalByRefObject,并实现您的服务器接口。这个具体类型是代理,可以在AppDomains之间进行序列化/反序列化,因为您的主应用程序知道它的定义。您不再需要从班级MarshalByRefObject中的ServerBase继承。

  // - MUST be serializable, and MUSNT'T use unknown types for main App
  [Serializable]
  public class Query 
  {
     ...
  }

  public interface IServerBase
   {  
       string Execute(Query q);
   }

  public abstract class ServerBase : IServerBase
  {
       public abstract string Execute(Query q);
  }

// Our CUSTOM PROXY: the concrete type which will be known from main App
[Serializable]
public class ServerBaseProxy : MarshalByRefObject, IServerBase
{
    private IServerBase _hostedServer;

    /// <summary>
    /// cstor with no parameters for deserialization
    /// </summary>
    public ServerBaseProxy ()
    {

    }

    /// <summary>
    /// Internal constructor to use when you write "new ServerBaseProxy"
    /// </summary>
    /// <param name="name"></param>
    public ServerBaseProxy(IServerBase hostedServer)
    {
        _hostedServer = hostedServer;
    }      

    public string Execute(Query q)
    {
        return(_hostedServer.Execute(q));
    }

}

注意:为了发送和接收数据,在IServer 中声明的每种类型必须可序列化(例如:带[Serializable]属性)

然后,您可以使用上一个链接“Loader class”中找到的方法。 这是我修改的Loader类,它在共享程序集中实例化具体类型,并为每个插件返回一个代理:

  /// <summary>
/// Source: https://stackoverflow.com/questions/16367032/appdomain-load-fails-with-filenotfoundexception
/// </summary>
public class Loader : MarshalByRefObject
{

    /// <summary>
    /// Load plugins
    /// </summary>
    /// <param name="assemblyName"></param>
    /// <returns></returns>
    public IPlugin[] LoadPlugins(string assemblyPath)
    {
        List<PluginProxy> proxyList = new List<PluginProxy>(); // a proxy could be transfered outsite AppDomain, but not the plugin itself ! https://stackoverflow.com/questions/4185816/how-to-pass-an-unknown-type-between-two-net-appdomains

        var assemb = Assembly.LoadFrom(assemblyPath); // use Assembly.Load if you want to use an Assembly name and not a path

        var types = from type in assemb.GetTypes()
                    where typeof(IPlugin).IsAssignableFrom(type)
                    select type;

        var instances = types.Select(
            v => (IPlugin)Activator.CreateInstance(v)).ToArray();

        foreach (IPlugin instance in instances)
        {
            proxyList.Add(new PluginProxy(instance));
        }
        return (proxyList.ToArray());
    }

}

然后,在主应用程序中 ,我还使用“dedpichto”和“James Thurley”的代码来创建AppDomain,instanciate并调用Loader类。然后我可以使用我的代理,因为它是我的插件,因为.NET由于MarshalByRefObject而创建了一个“透明代理”:

   /// <see cref="https://stackoverflow.com/questions/4185816/how-to-pass-an-unknown-type-between-two-net-appdomains"/>
public class PlugInLoader
{       

    /// <summary>
    /// https://stackoverflow.com/questions/16367032/appdomain-load-fails-with-filenotfoundexception
    /// </summary>
    public void LoadPlugins(string pluginsDir)
    {
        // List all directories where plugins could be
        var privatePath = "";
        var paths = new List<string>();
        List<DirectoryInfo> dirs = new DirectoryInfo(pluginsDir).GetDirectories().ToList();
        dirs.Add(new DirectoryInfo(pluginsDir));
        foreach (DirectoryInfo d in dirs)
            privatePath += d.FullName + ";";
        if (privatePath.Length > 1) privatePath = privatePath.Substring(0, privatePath.Length - 1);

        // Create AppDomain !
        AppDomainSetup appDomainSetup = AppDomain.CurrentDomain.SetupInformation;
        appDomainSetup.PrivateBinPath = privatePath; 

        Evidence evidence = AppDomain.CurrentDomain.Evidence;
        AppDomain sandbox = AppDomain.CreateDomain("sandbox_" + Guid.NewGuid(), evidence, appDomainSetup);

        try
        {
            // Create an instance of "Loader" class of the shared assembly, that is referenced in current main App
            sandbox.Load(typeof(Loader).Assembly.FullName);

            Loader loader = (Loader)Activator.CreateInstance(
                sandbox,
                typeof(Loader).Assembly.FullName,
                typeof(Loader).FullName,
                false,
                BindingFlags.Public | BindingFlags.Instance,
                null,
                null,
                null,
                null).Unwrap();

            // Invoke loader in shared assembly to instanciate concrete types. As long as concrete types are unknown from here, they CANNOT be received by Serialization, so we use the concrete Proxy type.

            foreach (var d in dirs)
            {
                var files = d.GetFiles("*.dll");
                foreach (var f in files)
                {
                    // This array does not contains concrete real types, but concrete types of "my custom Proxy" which implements IPlugin. And here, we are outside their AppDomain, so "my custom Proxy" is under the form of a .NET "transparent proxy" (we can see in debug mode) generated my MarshalByRefObject.
                    IPlugin[] plugins = loader.LoadPlugins(f.FullName);
                    foreach (IPlugin plugin in plugins)
                    {
                        // The custom proxy methods can be invoked ! 
                        string n = plugin.Name.ToString();
                        PluginResult result = plugin.Execute(new PluginParameters(), new PluginQuery() { Arguments = "", Command = "ENUMERATE", QueryType = PluginQueryTypeEnum.Enumerate_Capabilities });
                        Debug.WriteLine(n);
                    }                    
                }
            }
        }
        finally
        {
            AppDomain.Unload(sandbox);
        }
  }
}

找到一个可行的解决方案真的很难,但我们最终可以将我们具体类型的自定义代理实例保存在另一个AppDomain中,并使用它们就像它们在主应用程序中可用一样。

希望这个(巨大的答案)有所帮助!