.net 中的绑定重定向问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1751775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:34:40  来源:igfitidea点击:

Binding redirect problem in .net

.netbindingassemblies

提问by Suresh

I have a class library called "MyAssembly" that is internally referencing a.dll, b.dll of version 3.1.1.0; I have build the project which outputed MyAssembly.dll. On a different system (box) I have created a web application project and referenced the MyAssembly.dll. the new system has new versions of a.dll and b.dll 4.0.0; I used binding redirect in web.config like below. But still unable to compile the web application. it says missing assembly reference a.dll, version 3.1.1.0.

我有一个名为“MyAssembly”的类库,它在内部引用版本 3.1.1.0 的 a.dll、b.dll;我已经构建了输出 MyAssembly.dll 的项目。在不同的系统(框)上,我创建了一个 Web 应用程序项目并引用了 MyAssembly.dll。新系统有新版本的a.dll和b.dll 4.0.0;我在 web.config 中使用了绑定重定向,如下所示。但是仍然无法编译Web应用程序。它说缺少程序集引用 a.dll,版本 3.1.1.0。

Could any body help in solving this issue?

任何机构都可以帮助解决这个问题吗?

Thanks, Suresh

谢谢,苏雷什

回答by BradLaney

This totally worked for me. NOTE: You need NO namespaceon the configurationtag. And you MUST have a namespaceon your assemblyBindingtag.

这完全对我有用。注意:标签上不需要命名空间configuration。并且您的标签上必须有一个命名空间assemblyBinding

<assemblyBinding> Element for <runtime>

<assemblyBinding> Element for <runtime>

<!-- important: no namespace -->
<configuration> 
  <runtime>
    <!-- important, must have this namespace -->
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly>
        <assemblyIdentity name="Strongly.Named.Assembly" publicKeyToken="xxx" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Do both of those or else it will not read it. If it is giving an error that it cannot load anything but 2.0.0.0 in this example, then it is not picking up the config elements properly.

两者都做,否则它不会读取它。如果它给出一个错误,在这个例子中它不能加载除了 2.0.0.0 之外的任何东西,那么它没有正确地获取配置元素。

This also only works on strongly named assemblies. To find out if something is strongly named run the following command from the VC command window

这也仅适用于强命名程序集。要找出某些东西是否具有强命名功能,请从 VC 命令窗口运行以下命令

open (start menu > all programs > visual studio > visual studio tools > visual studio command prompt)

打开(开始菜单 > 所有程序 > Visual Studio > Visual Studio 工具 > Visual Studio 命令提示符)

Then run:

然后运行:

sn -vf "path-to-assembly.dll"

If it returns that it is valid, then it's strongly named.

如果它返回它是有效的,那么它就是强命名的。

source: http://blog.codingoutloud.com/2010/03/13/three-ways-to-tell-whether-an-assembly-dl-is-strong-named/

来源:http: //blog.codingoutloud.com/2010/03/13/three-ways-to-tell-whether-an-assembly-dl-is-strong-named/

回答by Manu

This should work.

这应该有效。

<runtime>  
 <dependentAssembly>  
   <assemblyIdentity name="MyAssembly" publicKeyToken="12233444"/>  
   <bindingRedirect oldVersion="3.1.1.0" newVersion="4.0.0.0"/>  
 </dependentAssembly>  
</runtime>  

Another suggestion: Remove the namespace from your configuration tag:

另一个建议:从配置标签中删除命名空间:

Instead of

代替

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

try

尝试

<configuration>

回答by Sajeev

You are using MyAssembly in your web application. The binding redirect will be used for this Assembly and not the assemblies which MyAssembly uses. Check the manifest for the MyAssembly.dll, it should be referring to the 3.1.1.0 versions of a.dll, hence the compiler error is shown. Build the MyAssembly with referring to a.dll of version 4.0.0.0 and then use the MyAssembly in your web application. This will work.

您正在 Web 应用程序中使用 MyAssembly。绑定重定向将用于此程序集,而不是 MyAssembly 使用的程序集。查看MyAssembly.dll的manifest,应该是a.dll的3.1.1.0版本,所以会出现编译错误。参考 4.0.0.0 版本的 a.dll 构建 MyAssembly,然后在您的 Web 应用程序中使用 MyAssembly。这将起作用。

回答by andrecarlucci

Try this way:

试试这个方法:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="a.dll"
                      publicKeyToken="{put a.dll publicKeytoken here}"
                      culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-99.99.99.99"
                     newVersion="4.0.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="b.dll"
                      publicKeyToken="{put b.dll publicKeytoken here}"
                      culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-99.99.99.99"
                     newVersion="4.0.0.0"/>
  </dependentAssembly>
</assemblyBinding>

Also, go to the references of your application, right click the a.dll and b.dll, go to properties and check if "Specific Version" is set to False.

另外,转到应用程序的引用,右键单击 a.dll 和 b.dll,转到属性并检查“特定版本”是否设置为 False。

Hope it helps.

希望能帮助到你。