简体   繁体   English

.NET中的程序集绑定问题

[英]Assembly binding problems in .NET

I am writing a .NET library that for various reasons cannot be registered in the GAC. 我正在编写一个.NET库,由于各种原因,该库无法在GAC中注册。 This dll (let's call it SDK.dll) depends on other DLLs in order to be loaded. 该dll(我们称其为SDK.dll)取决于要加载的其他DLL。

When writing a program that uses this SDK.dll, I noticed that my program failed loading the dll with a FileNotFoundException thrown. 当编写使用此SDK.dll的程序时,我注意到我的程序无法加载抛出FileNotFoundException的dll。 This happens because although I was able to find the referenced SDK.dll, the CLR failed to load its dependencies. 发生这种情况的原因是,尽管我能够找到引用的SDK.dll,但CLR无法加载其依赖项。

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 我找到解决问题的唯一方法是“复制本地” SDK.dll及其所有依赖项(由于部署问题而无法执行此操作),或者将程序编译到与SDK.dll相同的目录中

Is there a way to tell SDK.dll where to look for it's dependencies regardless of its location? 有没有办法告诉SDK.dll在哪里寻找其依赖项,而不管其位置在哪里? Maybe a SDK.dll.config file can help? 也许SDK.dll.config文件可以提供帮助?

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(). 在事件处理程序方法中,编写逻辑以查找程序集并使用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;
}

Re: Snooganz 回复:Snooganz

You probably mean to use Load File instead of Load, like so: 您可能打算使用“加载文件”而不是“加载”,如下所示:

Assembly theAssembly = Assembly.LoadFile(assemblyFullName);

Assembly.Load will probably get you into an infinite loop =) Assembly.Load可能会使您陷入无限循环=)

to register your assembly in GAC it must have be signed with a strong name . 要在GAC中注册您的程序集,必须使用强名签名。

If it depends on other assemblies, those should be in GAC to. 如果依赖其他程序集,则应在GAC中进行。

BeowulfOF Beowulf

You can't GAC the SDK but could you GAC the dependancies? 您不能GAC SDK,但是您可以GAC依赖吗?

Also Read this msdn article on assembly binding: 另请阅读此msdn关于程序集绑定的文章:

http://msdn.microsoft.com/en-us/library/efs781xb(VS.71).aspx http://msdn.microsoft.com/zh-CN/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. 如果程序集是强命名的,则可以使用代码库,如果不是,则代码库必须是isntall的子目录,这将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM