c# – AssemblyResolve没有为依赖关系触发

我正在与AssenblyResolve活动一段时间.我搜索了stackoverflow,并进行了其他谷歌搜索,并尝试所有我认为是相关的.这里的链接越接近我的问题(在我看来):

> AssemblyResolve is not invoked and FileNotFoundException is thrown during serialization
> Where to handle AssemblyResolve event in a class library?

我有一个Bootstrapper类与静态方法(我将删除我们拥有的线程安全代码,只是为了清楚起见:

public static void Initialize()
{
    AppDomain.CurrentDomain.AssemblyResolve += Customresolve;
}

private static Assembly Customresolve(object sender,ResolveEventArgs args)
{
    // There is a lot code here but basicall what it does.
    // Is determining which architecture the computer is running on and
    // extract the correct embedded dll (x86 or x64). The code was based
    // on milang on GitHub (https://github.com/milang/P4.net). And it's the same
    // purpose we want to be able to load the x86 or x64 version of the perforce dll
    // but this time with the officially Perforce supported p4api.net.
    // Once the dll is extracted we assign it to the boostrapper
    Bootstrapper._p4dnAssembly = Assembly.LoadFile(targetFileName);

    // Make sure we can satisfy the requested reference with the embedded assembly (Now extracted).
    AssemblyName reference = new AssemblyName(args.Name);
    if (AssemblyName.ReferenceMatchesDeFinition(reference,Bootstrapper._p4dnAssembly.GetName()))
    {
        return Bootstrapper._p4dnAssembly;
    }
}

我能够使代码工作,如果我有一个简单的类与一个主要的方法和静态构造函数.静态构造函数只是调用Boostrapper.Initialize()方法.
之后,我可以使用我的图书馆,它按预期工作:

public static class Test
{
    static test()
    {
        Bootstrapper.Initialize();
    }

    public static void Main()
    {
         // Using the library here is working fine. The AssemblyResolve event was
         // fired (confirmed by a breakpoint in Visual Studio)
    }
}

我遇到的问题是如果至少有一层依赖关系.基本上代码保持不变,但这次我的库的代码在另一个库中:

public static class Test
{
    static test()
    {
        Bootstrapper.Initialize();
    }

    public static void Main()
    {
        Class1 myClass = new Class1();

        // The following line is using the code of the extracted library,but
        // The AssemblyResolve event is not fired (or fired before I register the  
        // callback) and therefore the library is not found : result  
        // BadImageFormatException() error Could not load libary because one
        myClass.Connect();
    }
}

听起来像我之前说过的链接的第2条说明了我所看到的东西,但它不起作用. AssemblyResove回调中的Visual Studio断点永远不会被击中.

任何想法发生了什么?

弗朗西斯

解决方法

我知道已经有一段时间了,因为这个问题被提出和回答,但是我想补充一下我的想法(因为我只是浪费了几个小时,也许感谢这个别人不必)

问题基本上是这样一个事实:应用程序正在尝试解决在该方法开始时执行方法所需的所有程序集:

static void main(string[] args)
{
    // <-- here the app tries to resolve MyAssembly
    // and as MyAssembly.Class1 is not found,the app crashes

    // this next line is never called:
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);

    // class contained in an assemnly that we need to resolve
    MyAssembly.Class1 myClass = new MyAssembly.Class1();
}

这就是为什么上面会崩溃的:ResolveAssembly事件处理程序永远不会被调用,因为它从来没有被挂接.

这也是为什么下面的解决方案工作(由OP发布):

static void main(string[] args)
{
    Initialize();
    RealMain();
}

static void Initialize()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
}

static void RealMain()
{
   // <-- here the app tries to resolve MyAssembly
   // class contained in an assemnly that we need to resolve      
   MyAssembly.Class1 myClass = new MyAssembly.Class1();
   // and everything is OK
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...