Od pewnego czasu walczę z wydarzeniem AssenblyResolve. Szukałem stackoverflow i zrobiłem inne google i wypróbowałem wszystko, co moim zdaniem było trafne. Oto linki będące bliżej mojego problemu (moim zdaniem):AssemblyResolve nie zwolniony dla zależności
Mam klasy Bootstrapper z metodą statyczną (usunę kod zabezpieczający wątek, który mamy, tylko ze względu na jasność:
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;
}
}
Udało mi się sprawić, że kod zadziała, jeśli mam prostą klasę z główną metodą i konstruktorem statycznym. Konstruktor statyczny wywołuje po prostu metodę Boostrapper.Initialize(). Po tym, mogłem użyć moją bibliotekę i działa zgodnie z oczekiwaniami:
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)
}
}
Mam problem jest, jeśli istnieje co najmniej jedna warstwa uzależnienia. Zasadniczo kod pozostaje taka sama, ale tym razem mój kod biblioteki znajduje się wewnątrz innej biblioteki:
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();
}
}
Brzmi jak # 2 z linków podanych wcześniej mam wyjaśnić to, co widzę, ale to nie działa . Punkt przerwania Visual Studio wywołania zwrotnego AssemblyResove nigdy nie jest trafiony.
Masz pojęcie o tym, co się dzieje?
Francis