Referenced assembly loading outside appbase

  • Thread starter Thread starter David McCabe
  • Start date Start date
D

David McCabe

I have an ATL component which uses MC++ /clr to talk to .NET components. Due
to the limitations of the (native) client, the COM server is located outside
the appbase, and there can be no configuration files.

Now, when the component is instantiated, all is well, right up to the point
at which I instantiate an object in another .NET assembly (located in the
same directory as the ATL DLL). Then I predictably get a
FileNotFoundException - because it's looking in the appbase of the client,
not the server.

I've tried using AppendPrivatePath, and I predictably get the "outside
appbase" error, as usual. So, I concluded that the only way to do this is to
preload the assemblies on which I depend.

Why doesn't this work, then?

String *dir =
Path::GetDirectoryName(System::Reflection::Assembly::GetCallingAssembly()->Location);
String *filename = Path::Combine(dir, String::Concat(name, S".dll"));
Assembly *assemblyFromFile = Assembly::LoadFrom(filename);
Assembly *assemblyFromStrongName =
AppDomain::CurrentDomain->Load(assemblyFromFile->FullName);

It's illustrative, but the principle is the same. I can load the assembly
just fine, but that doesn't resolve the dependency in the application. When
the loader tries to load the dependency - like the last line in those four -
as I understand it, it should use the previously loaded assembly, rather
than searching for it anew. It doesn't.

Any light?

David
 
Ok, you have following options
1. Register your COM server in same dir as clien
2. Register it whatever, but insert it to GAC(strong name will be needed
3. Register it in any dir UNDER app dir and add appropriate bind directive to app config fil
Good luck!
 
Hook the AssemblyResolve event and use LoadFrom on the assemblies that need
to be loaded - you can cache the result so that you don't call LoadFrom each
time it needs to resolve the assembly. One problem you may be running into
is that even though you have the assembly loaded, you loaded it into the
LoadFrom context, which is different from the Load context, and the runtime
will not use the LoadFrom context to automatically resolve references.
 
Back
Top