【问题标题】:Under what circumstanses will AppDomain.DoCallback() fail?AppDomain.DoCallback()在什么情况下会失败?
【发布时间】:2012-09-24 12:07:47
【问题描述】:

我正在尝试使用AppDomain 在运行时加载和卸载程序集。在实现程序集加载之前,我试图让 MSDN 上的示例在我的应用程序中工作,但我遇到了问题 - DoCallback-invokation 失败并出现异常

无法加载文件或程序集'[MyPluginAssembly],版本=1.0.0.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。这 系统找不到指定的文件。

我的程序集 ([MyPluginAssembly]) 正在运行,由主机应用程序加载(即它是一个插件)。插件AppDomain 似乎是应用程序域(即它没有在单独的域中进行沙盒处理)。我已尝试在新域中加载条目/调用/执行程序集以确保已加载 [MyPluginAssembly],但即使这些调用返回非 null,我仍然会收到上述异常。

我使用的代码(如example on MSDN + 加载“父”组件的代码):

public class PingPong : MarshalByRefObject
{
    private string greetings = "PING!";

    public static void Main()
    {
        AppDomain otherDomain = AppDomain.CreateDomain("otherDomain");
        // All of these Load()-calls returns non-null
        Assembly entryAssembly = otherDomain.Load(Assembly.GetEntryAssembly().GetName());
        Assembly callingAssembly = otherDomain.Load(Assembly.GetCallingAssembly().GetName());
        Assembly executingAssembly = otherDomain.Load(Assembly.GetExecutingAssembly().GetName());

        PingPong pp = new PingPong();
        pp.MyCallBack();
        pp.greetings = "PONG!";
        otherDomain.DoCallBack(new CrossAppDomainDelegate(pp.MyCallBack));

        // Output: 
        //   PING! from defaultDomain 
        //   PONG! from defaultDomain
    }

    // Callback will always execute within defaultDomain due to inheritance from 
    // MarshalByRefObject 
    public void MyCallBack()
    {
        string name = AppDomain.CurrentDomain.FriendlyName;
        if (name == AppDomain.CurrentDomain.SetupInformation.ApplicationName)
        {
            name = "defaultDomain";
        }
        Console.WriteLine(greetings + " from " + name);
    }
}

什么情况会导致我得到异常?

【问题讨论】:

  • 哪一行给出了异常?
  • @DanielHilgarth: otherDomain.DoCallBack(new CrossAppDomainDelegate(pp.MyCallBack));
  • 使用 fuslogvw.exe 解决程序集解析问题。

标签: c# .net appdomain .net-assembly


【解决方案1】:

您应该查看CreateInstanceFromAndUnwrap 方法。它适用于像您这样的沙盒加载项方案。 System.AddIn (MAF) 正在使用此方法加载加载项管道段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多