views:

787

answers:

5

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?

A: 

Hi,

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

BeowulfOF
The problem is that I am not allowed to register the DLL in the GAC...
Sakin
A: 

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.

JoshBerke
A: 

Try to use DEVPATH env variable.

http://msdn.microsoft.com/en-us/library/cskzh7h6.aspx

Leonidius
+4  A: 

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;
}
Snooganz
A: 

Re: Snooganz

You probably mean Assembly theAssembly = Assembly.Load**File**(assemblyFullName);

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

MauriceL
As long as the assembly pointed to by "assemblyFullName" exists there is no infinite loop. The AssemblyResolve event is only fired if the assembly cannot be found, so it wouldn't be fired again in that call to Assembly.Load(assemblyFullName). The sample code was simply to demonstrate how to handle the event...You should of course insert error checking where appropriate (such as checking for the assemblies existence).
Snooganz