Question

I am writing a .NET library that for various reasons cannot be registered in the GAC. This dll (let's call it SDK.dll) depends on other DLLs in order to be loaded.

When writing a program that uses this SDK.dll, I noticed that my program failed loading the dll with a FileNotFoundException thrown. This happens because although I was able to find the referenced SDK.dll, the CLR failed to load its dependencies.

The only way I found to solve the problem is to "Copy Local" the SDK.dll and all its dependencies (something I can't do because of deployment problems), or compiling my program into the same directory as SDK.dll

Is there a way to tell SDK.dll where to look for it's dependencies regardless of its location? Maybe a SDK.dll.config file can help?

Was it helpful?

Solution

You can handle this at runtime by subscribing to this event:

AppDomain.CurrentDomain.AssemblyResolve

It's fired when the runtime fails to resolve an assembly. In your event handler method, write your logic to find the assembly and load it using Assembly.LoadFrom(). It would look something like this:

public SDKClass()
{
  AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(FindAssembly);
}

private Assembly FindAssembly(object sender, ResolveEventArgs args)
{
  string assemblyPath = "c:\PathToAssembly";
  string assemblyName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
  string assemblyFullName = Path.Combine(assemblyPath, assemblyName);

  Assembly theAssembly = Assembly.Load(assemblyFullName);

  return theAssembly;
}

OTHER TIPS

to register your assembly in GAC it must have be signed with a strong name.

If it depends on other assemblies, those should be in GAC to.

BeowulfOF

You can't GAC the SDK but could you GAC the dependancies?

Also Read this msdn article on assembly binding:

http://msdn.microsoft.com/en-us/library/efs781xb(VS.71).aspx

if your assemblies are strong named you can use code base, if they are not the code base has to be a child directory of your isntall which won't help.

Re: Snooganz

You probably mean to use LoadFile instead of Load, like so:

Assembly theAssembly = Assembly.LoadFile(assemblyFullName);

Assembly.Load will probably get you into an infinite loop =)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top