引用Microsoft.Build和System.IO.Compression时缺少MissingMethodException [英] MissingMethodException when referencing Microsoft.Build and System.IO.Compression

查看:458
本文介绍了引用Microsoft.Build和System.IO.Compression时缺少MissingMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经典桌面" .NET项目,该项目引用了NuGet的Microsoft.Build 15.1和.NET SDK/GAC的System.IO.Compression/System.IO.Compression.FileSystem.

I have a "Classic Desktop" .NET project which references Microsoft.Build 15.1 from NuGet and System.IO.Compression/System.IO.Compression.FileSystem from the .NET SDK / GAC.

我正在尝试将其升级到Microsoft.Build 15.3.

I'm trying to upgrade this to Microsoft.Build 15.3.

Microsoft.Build 15.3引入了对System.IO.Compression 4.1.2.0的依赖. .NET Framework中System.IO.Compression的版本为4.0.0.0.

Microsoft.Build 15.3 introduces a dependency on System.IO.Compression 4.1.2.0. The version of System.IO.Compression in the .NET Framework is 4.0.0.0.

如果我这样编译,则会收到有关无法解决程序集冲突的警告,但是我的代码可以正常工作

If I compile like this, I get a warning about being unable to resolve assembly conflicts, but my code works:

警告MSB3277:发现无法解决的相同从属程序集的不同版本之间存在冲突.将日志详细程度设置为详细"时,这些参考冲突会在构建日志中列出.

warning MSB3277: Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

将构建日志的详细程度设置为详细"将产生以下输出:

Setting the build log verbosity to Detailed yields this output:

1>  There was a conflict between "System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
1>      "System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" was chosen because it was primary and "System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" was not.
1>      References which depend on "System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" [C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.IO.Compression.dll].
1>          C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.IO.Compression.dll
1>            Project file item includes which caused reference "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.IO.Compression.dll".
1>              System.IO.Compression
1>      References which depend on "System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" [C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\System.IO.Compression.dll].
1>          C:\Temp\CompressionMissingMethod\packages\Microsoft.Build.15.3.409\lib\net46\Microsoft.Build.dll
1>            Project file item includes which caused reference "C:\Temp\CompressionMissingMethod\packages\Microsoft.Build.15.3.409\lib\net46\Microsoft.Build.dll".
1>              Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1988,5): warning MSB3277: Found conflicts between different versions of the same dependent assembly that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.

我可以通过使用NuGet引用替换对System.IO.Compression的SDK/GAC引用来解决警告.但是,一旦执行此操作,当JIT到达该方法时,使用ZipFile的代码就会与MissingMethodException崩溃.

I can resolve the warning by replacing the SDK/GAC reference to System.IO.Compression with a NuGet reference. As soon as I do this, however, code using ZipFile crashes with a MissingMethodException when the JIT gets to that method.

未处理的异常:System.MissingMethodException:找不到方法:'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'. 在CompressionMissingMethod.Program.Main(String [] args)

Unhandled Exception: System.MissingMethodException: Method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'. at CompressionMissingMethod.Program.Main(String[] args)

我整理了一个非常小的 repro案例,它说明了这种行为(请查看分支).

I've put together a very minimal repro case which illustrates this behaviour (check out the branches).

有没有办法同时引用Microsoft.Build 15.3和System.IO.Compression而没有错误,警告或异常?

Is there any way to reference both Microsoft.Build 15.3 and System.IO.Compression with no errors, warnings, or exceptions?

推荐答案

要修复MSB3277警告指示的问题(以及缺少方法异常问题)-您需要将绑定重定向添加到您的app.config文件,以便所有绑定被重定向到最新版本(在这种情况下为4.1.2.0):

To fix problem indicated by MSB3277 warning (as well as missing method exception problem) - you need to add binding redirect to your app.config file, so that all bindings are redirected to the last version (4.1.2.0 in this case):

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

因此,请从nuget安装Microsoft.Build 15.3,再安装System.IO.Compression(这会将您的状态移至MissingMethodException情况),然后在上面添加重定向,异常将消失,并且一切正常,而不会出现异常或警告.

So, install Microsoft.Build 15.3, install System.IO.Compression from nuget (which moves your state to that of MissingMethodException case), then add redirect above and exception will go away and things will work without exceptions or warnings.

在注释中建议的替代方法是将所有软件包更新到最新版本-由于相同的原因,该方法也有效-绑定重定向.如果您更新所有软件包,然后进行重建-很多重定向将添加到您的配置中,包括System.IO.Compression的重定向(但版本为4.2.0.0).请注意,这些重定向不会添加到您的app.config中,而是直接添加到输出的YourAppName.exe.config文件中.

Alternative suggested in comments is to update all packages to last versions - this also works because of the same reason - binding redirect. If you update all packages then rebuild - a lot of redirects will be added to your config, including redirect for System.IO.Compression (but to version 4.2.0.0). Note that those redirects will be added not to your app.config but directly to the output YourAppName.exe.config file.

这篇关于引用Microsoft.Build和System.IO.Compression时缺少MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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