I have a VS Add-In that's using a BinaryFormatter to deserialize an object. To resolve the type of this object, it's calling Assembly.Load(objectTypeFullName) but it's triggering an exception because Assembly.Load cannot find the assembly in any of the places it's searching on. The given assembly is sibling to the add-in assembly, but it seems that Assembly.Load() can't find it there.
A possibl开发者_运维百科e solution would be to determine where Assembly.Load should look for assemblies.
What should I do?
PS: I'm trying not to put this assembly on GAC because I would need to update it everytime I recompile the assembly.
You can use AppDomainSetup.PrivateBinPath to add additional private search paths. This can be retrieved via AppDomain.SetupInformation.
Another option is to subscribe to AppDomain.AssemblyResolve in order to override the behavior when it fails to find your assembly.
Here's a code snippet showing how AssemblyResolve might be used to resolve your assembly (as per Reed Copey's answer):
// register to listen to all assembly resolving attempts:
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
// Check whether the desired assembly is already loaded
private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
string desiredAssmebly = args.Name;
if (desiredAssembly.Equals("NameUsedToLoadMyAssembly")){
return Assembly.LoadFrom(myAssemblyPath);
}
return null;
}
Also, note that the MSDN page for AssemblyResolve states that:
Beginning with the .NET Framework version 4, the ResolveEventArgs.RequestingAssembly property returns the assembly that requested the assembly load that could not be resolved...
This can be used if you know your assembly's location relative to that of the requesting assembly.
If you are just trying to determine where the assembly loader is trying to load your dll from, I recommend that you turn on the fusion logs. Doing so will enable you to get output that will show you each path that was checked for the corresponding dll.
There is an MSDN article on how to configure the fusion logs, and a useful article by Suzanne Cook on how to debug loading failures. If you turn on LogFailures
and you should only get output for assemblies that failed to be loaded.
精彩评论