Сейчас я боюсь события AssenblyResolve. Я искал stackoverflow и сделал другой поиск в Google и пробовал все, что, по моему мнению, было актуальным. Вот ссылки, которые ближе к моей проблеме (на мой взгляд):
-
AssemblyResolve не вызывается и FileNotFoundException бросается во время сериализации
-
Где обрабатывать событие AssemblyResolve в библиотеке классов?
У меня есть класс 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 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 ссылок, которые я ранее говорил, объясняет, что я вижу, но это не работает. Точка прерывания Visual Studio на обратном вызове AssemblyResove никогда не ударяется.
Любая идея о том, что происходит?
Фрэнсис