我现在正与AssenblyResolve事件斗争一段时间。我搜索过堆栈溢出,做了其他谷歌搜索,并尝试了所有我认为相关的内容。下面的链接更接近我的问题(在我看来):
我有一个带有to静态方法的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;
}
}如果I有一个带有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();
}
}听起来像是我之前说过的第二种链接,解释我看到的内容,但它不起作用。AssemblyResove回调的Visual断点从未被击中。
知道怎么回事吗?
弗朗西斯
发布于 2012-10-30 18:27:06
有人回答了,但答案被删除了。所以我不能按答案做标记。基本上,代码不需要在"main“方法之外的事实是有效的。从一个新项目开始,解决这个问题,所以我想我的dll可能有一些问题(可能是x86 dll在x64文件夹中,或者反之亦然)。
static void main(string[] args)
{
Boostrapper.Initialize();
RealMain();
}
static void RealMain()
{
Class1 myClass = new Class1();
myClass.Connect();
}发布于 2014-09-23 09:00:14
我知道这个问题已经有一段时间没有被提出和回答了,但我还是想补充一下我对这个问题的看法(因为我只是在这个问题上浪费了几个小时,也许多亏了这个问题,其他人就不必这么做了)
问题主要在于,应用程序试图解决在该方法开始时执行该方法所需的所有程序集:
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
}https://stackoverflow.com/questions/13088199
复制相似问题