用于编译的.NET绑定重定向

Lee*_*Lee 11 .net c#

当我尝试编译一个使用已部署到我们客户端的文件的实用程序时,我收到以下错误.

程序集'*A*版本2.0.1.2'使用'*B*版本1.1.39.0',其版本高于引用程序集'*B*版本1.1.32.0'.

我们的客户端可以使用这些DLL没问题,因为我们有一个绑定重定向配置文件,它在运行时生效:

<dependentAssembly>
  <assemblyIdentity name="*B*" publicKeyToken="..." culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.1.32.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

为了给出一些背景知识,DLL存在于单独的解决方案中,因此一些引用是文件引用而不是项目引用,这只是我必须要忍受的东西!

是否有任何等效的绑定重定向适用于编译时?

我已经尝试使用调试DLL(版本1.0.0.0)进行编译,源代码回滚到上面的相关版本,但是我在运行时遇到以下错误:

定位的程序集的清单定义与程序集引用不匹配

也许构建服务器的配置与我的机器不同,但无论如何这似乎都不起作用......

Evk*_*Evk 7

我怀疑是否有可能按照你的意愿"修复"它.如果您阅读该编译错误的文档(https://msdn.microsoft.com/en-us/library/416tef0c.aspx),您将看到您可以更新代码以使用相同的版本,或者在此期间引用这两个版本编译(在您的情况下不是一个选项).

想象一下,版本1.0.0.0包含方法MyMethod(),但版本1.0.0.1包含MyMethod(字符串),第一个版本由程序集A使用,第二个版本由您正在编译的程序集使用.您希望编译器如何解决此问题?在运行时,当您使用绑定重定向时,仍将仅加载一个版本的程序集.在这里,您没有程序集A的代码(您正在引用它,而后者又引用了MyMethod),并且对程序集1.0.0.0的引用嵌入在A的清单中.

长话短说 - 我想只有办法为你解决它是使用汇编A,它引用与你相同版本的B.


Han*_*ant 6

这是一个非常常见的事故.Nuget包最有可能是麻烦制造者,log4net和NewtonSoft.Json位于该列表的顶部.现代版本的MSBuild知道如何解决这个问题.当您使用工具>选项>项目和解决方案>构建和运行> MSBuild项目构建输出verbosity =详细时,您可以看到的内容.

我会在VS2015上展示它的样子,再现你确切的问题场景.它开始在ResolveAssemblyReference任务中变得有趣:

1>Task "ResolveAssemblyReference"
...
1>  Primary reference "B, Version=1.1.32.0, Culture=neutral, PublicKeyToken=null".
1>      Resolved file path is "c:\projects2\BCopy\bin\Debug\B.dll".
1>      Reference found at search path location "{HintPathFromItem}".
1>      Found related file "c:\projects2\BCopy\bin\Debug\B.pdb".
1>      The ImageRuntimeVersion for this reference is "v4.0.30319".
1>  Primary reference "A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null".
1>      Resolved file path is "c:\projects2\A\bin\Debug\A.dll".
1>      Reference found at search path location "{HintPathFromItem}".
1>      Found related file "c:\projects2\A\bin\Debug\A.pdb".
1>      The ImageRuntimeVersion for this reference is "v4.0.30319".
Run Code Online (Sandbox Code Playgroud)

还有一堆.NET Framework程序集.在任务结束时,它注意到A依赖于新版本的B:

1>  Dependency "B, Version=1.1.39.0, Culture=neutral, PublicKeyToken=null".
1>      Resolved file path is "c:\projects2\B\bin\Debug\B.dll".
1>      Reference found at search path location "c:\projects2\B\bin\Debug".
1>          For SearchPath "c:\projects2\B\bin\Debug".
1>          Considered "c:\projects2\B\bin\Debug\B.winmd", but it didn't exist.
1>      Required by "A, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL".
1>      Found related file "c:\projects2\B\bin\Debug\B.pdb".
1>      This reference is not "CopyLocal" because it conflicted with another reference with the same name and lost the conflict.
1>      The ImageRuntimeVersion for this reference is "v4.0.30319".
1>  There was a conflict between "B, Version=1.1.32.0, Culture=neutral, PublicKeyToken=null" and "B, Version=1.1.39.0, Culture=neutral, PublicKeyToken=null".
1>      "B, Version=1.1.32.0, Culture=neutral, PublicKeyToken=null" was chosen because it was primary and "B, Version=1.1.39.0, Culture=neutral, PublicKeyToken=null" was not.
1>      References which depend on "B, Version=1.1.32.0, Culture=neutral, PublicKeyToken=null" [c:\projects2\BCopy\bin\Debug\B.dll].
1>          c:\projects2\BCopy\bin\Debug\B.dll
1>            Project file item includes which caused reference "c:\projects2\BCopy\bin\Debug\B.dll".
1>              B, Version=1.1.39.0, Culture=neutral, processorArchitecture=MSIL
1>      References which depend on "B, Version=1.1.39.0, Culture=neutral, PublicKeyToken=null" [c:\projects2\B\bin\Debug\B.dll].
1>          c:\projects2\A\bin\Debug\A.dll
1>            Project file item includes which caused reference "c:\projects2\A\bin\Debug\A.dll".
1>              A, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL
1>Done executing task "ResolveAssemblyReference".
Run Code Online (Sandbox Code Playgroud)

请注意它如何尝试决定是使用版本1.1.32.0还是1.1.39.0.它更喜欢旧版本,因为它是"主要参考".换句话说,您的项目引用它并且1.1.39.0不太重要,因为它是A的间接引用.

该项目建立清洁,没有任何投诉.为什么这对你不起作用是完全不清楚的,命名你的VS版本非常重要.我不记得确切地说这个功能何时添加,它是不久前的.在VS2010或VS2012附近.

因此,尝试通过将您在计算机上获得的构建跟踪与我发布的内容进行比较来获得成功.如果它只是一个普通的旧VS版本问题,那么通过更新它你当然会更加领先.