程序集绑定和重定向 [英] Assembly binding and redirect

查看:139
本文介绍了程序集绑定和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个引用DLL的EXE-在此示例中,我将其称为 TestDLL.dll .
EXE用C#编写,而DLL用VB .Net编写.

我创建了DLL的演示程序集版本-例如-TestDLL.dll版本1.0.0.0.
我想使用对演示版DLL(1.0.0.0)的引用来编译EXE.之后-我希望EXE使用相同的DLL,但是我将放入任何版本的GAC中.
为此,我将DLL引用的 复制本地" 属性设置为 FALSE ./p>

例如,我的目标是-编译后,我将GAC TestDLL.dll放入程序集版本2.1.6.0中,EXE将使用程序集重定向绑定找到它.为此,我使用了一个配置文件.我使用此链接来创建它:
http://msdn.microsoft.com/zh- us/library/7wd6ex19(v = vs.71).aspx

所以我的配置文件看起来像这样:

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="myAssembly"
                           publicKeyToken="32ab4ba45e0a69a1"
                           culture="en-us" />
         <!-- Assembly versions can be redirected in application, publisher policy, or machine configuration files. -->
         <bindingRedirect oldVersion="1.0.0.0"
                          newVersion="2.1.6.0"/>
       </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

问题是做完所有这些之后,我运行了EXE,并且在访问dll时出现了著名的错误: System.IO.FileNotFoundException:无法加载文件或程序集"TestDLL,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 9d8162944bd6fdc7"或其依赖项之一.该系统找不到指定的文件. 文件名:"TestDLL,版本= 1.0.0.0,区域性=中性,PublicKeyToken = 9d8162944bd6fdc7"

这意味着,EXE无法找到我引用的原始DLL . 我知道我可以引用" GAC或使用反射,但是我不想-因为该EXE应该只能以这种方式工作.

有人知道问题出在哪里以及如何解决吗?
谢谢

解决方案

请确保已设置正确的publicKeyToken.在显示的代码中,您正在使用publicKeyToken="32ab4ba45e0a69a1",它是MSDN链接中的公共密钥令牌.显然,这不是您程序集的公钥令牌.为此,您需要使用相同的密钥对两个程序集(1.0.0.0和2.1.6.0)进行签名.要提取公钥令牌,可以使用 sn.exe 工具或查看异常堆栈跟踪您得到的(它告诉您publicKeyToken="9d8162944bd6fdc7"):

sn.exe -Tp myassembly.dll

但是,如果编译可执行文件所针对的程序集没有使用相同的密钥签名,那么它将无法正常工作.

我还看到您已经设置了culture="en-us",但是您的程序集是否使用这种文化?您也可以尝试culture="Neutral".

最后,请确保已将适当版本的程序集部署到GAC中.

I have an EXE that reference a DLL - for this example I'll call it TestDLL.dll.
The EXE is written in C# and the DLL is written in VB .Net.

I created a demo assembly version of the DLL - for example - TestDLL.dll version 1.0.0.0.
I want to compile the EXE with a reference to the demo version DLL (1.0.0.0). Afterwards - I want the EXE to use the same DLL, but the one I'll put in the GAC, of any version.
In order to do that, I set the "Copy Local" property of the DLL's reference to FALSE.

My goal is for example - after compiling, I'll put in the GAC TestDLL.dll with assembly version 2.1.6.0, and the EXE will find it using the assembly redirect binding. For that, I used a config file. I used this link to create it:
http://msdn.microsoft.com/en-us/library/7wd6ex19(v=vs.71).aspx

So my config file looks about like this:

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="myAssembly"
                           publicKeyToken="32ab4ba45e0a69a1"
                           culture="en-us" />
         <!-- Assembly versions can be redirected in application, publisher policy, or machine configuration files. -->
         <bindingRedirect oldVersion="1.0.0.0"
                          newVersion="2.1.6.0"/>
       </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

The problem is that after doing all that, I run the EXE and when accessing the dll, I get the famous error: System.IO.FileNotFoundException: Could not load file or assembly 'TestDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9d8162944bd6fdc7' or one of its dependencies. The system cannot find the file specified. File name: 'TestDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9d8162944bd6fdc7'

Meaning, the EXE can't find the original DLL I referenced to. I know that I can just "reference" the GAC or use reflection, but I don't want to - since this EXE is supposed to work only this way.

Does anyone know what's the problem and how to fix it?
Thanks

解决方案

Make sure that you have set the proper publicKeyToken. In the code you have shown you are using publicKeyToken="32ab4ba45e0a69a1" which is the public key token from the MSDN link. This obviously is not the public key token of your assembly. For this to work you need to have both assemblies (1.0.0.0 and 2.1.6.0) signed with the same key. To extract the public key token you could use the sn.exe tool or looked at the exception stack trace you are getting (it is telling you that publicKeyToken="9d8162944bd6fdc7"):

sn.exe -Tp myassembly.dll

But if the assembly that the executable was compiled against was not signed with the same key this won't work.

Also I see that you have set the culture="en-us" but does your assembly use this culture? You could also try culture="Neutral".

Finally make sure that you have deployed the proper version of the assembly into the GAC.

这篇关于程序集绑定和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆