gpt4 book ai didi

c# - AssemblyResolve 未针对依赖项触发

转载 作者:可可西里 更新时间:2023-11-01 03:05:49 36 4
gpt4 key购买 nike

我现在正在为 AssenblyResolve 事件苦苦挣扎一段时间。我搜索了 stackoverflow 并进行了其他谷歌搜索,并尝试了所有我认为相关的内容。以下是更接近我的问题的链接(在我看来):

  1. AssemblyResolve is not invoked and FileNotFoundException is thrown during serialization

  2. 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;
}
}

如果我有一个带有 main 方法和静态构造函数的简单类,我就能使代码正常工作。静态构造函数只是调用 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
}

关于c# - AssemblyResolve 未针对依赖项触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13088199/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com