Sto lottando con l'evento AssenblyResolve per un po 'di tempo. Ho cercato StackOverflow e fatto altri googling e provato tutto ciò che penso fosse rilevante. Ecco i link che sono il più vicino al mio problema (a mio parere):AssemblyResolve non licenziato per le dipendenze
Ho una classe di avvio automatico con il metodo statico (toglierò il codice thread safe che abbiamo, solo per motivi di chiarezza:
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;
}
}
Sono riuscito a far funzionare il codice se ho una classe semplice con un metodo main e un costruttore statico. Il costruttore statico chiama semplicemente il metodo Boostrapper.Initialize(). Dopo di che, ho potuto usare la mia biblioteca e stava funzionando come previsto:
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)
}
}
Il problema che ho è se c'è almeno uno strato di dipendenza. In pratica il codice rimane lo stesso, ma questa volta il mio codice della biblioteca si trova all'interno di un'altra libreria:
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();
}
}
Suona come # 2 dei link che ho affermato in precedenza spiegare quello che sto vedendo, ma non funziona . Il punto di interruzione di Visual Studio sul callback AssemblyResove non viene mai colpito.
Qualche idea di cosa sta succedendo?
Francesco