引用 Microsoft.Build 和 System.IO.Compression 时出现 MissingMethodException
MissingMethodException when referencing Microsoft.Build and System.IO.Compression
我有一个 "Classic Desktop" .NET 项目,它引用了来自 NuGet 的 Microsoft.Build
15.1 和来自 .NET SDK / GAC 的 System.IO.Compression
/System.IO.Compression.FileSystem
。
我正在尝试将其升级到 Microsoft.Build
15.3.
Microsoft.Build
15.3 引入了对 System.IO.Compression
4.1.2.0 的依赖。 .NET Framework 中 System.IO.Compression
的版本是 4.0.0.0.
如果我这样编译,我会收到无法解决程序集冲突的警告,但我的代码有效:
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.
将构建日志详细程度设置为详细会产生此输出:
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 Studio17\Enterprise\MSBuild.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 Studio17\Enterprise\MSBuild.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.
我可以通过将 SDK/GAC 对 System.IO.Compression
的引用替换为 NuGet 引用来解决警告。但是,一旦我这样做,当 JIT 到达该方法时,使用 ZipFile
的代码就会崩溃并出现 MissingMethodException
。
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 case 来说明这种行为(查看分支)。
有什么方法可以同时引用 Microsoft.Build
15.3 和 System.IO.Compression
而没有错误、警告或异常吗?
要解决 MSB3277 警告指示的问题(以及缺少方法异常问题)- 您需要将绑定重定向添加到 app.config
文件,以便所有绑定都重定向到最新版本(4.1.在这种情况下为 2.0):
<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>
因此,安装 Microsoft.Build 15.3,从 nuget 安装 System.IO.Compression(这会将您的状态移动到 MissingMethodException 情况下的状态),然后在上面添加重定向,异常将消失,并且一切正常或警告。
评论中建议的替代方法是将所有包更新到最新版本——这也有效,原因相同——绑定重定向。如果您更新所有包然后重建 - 很多重定向将添加到您的配置中,包括 System.IO.Compression 的重定向(但到版本 4.2.0.0)。请注意,这些重定向不会添加到您的 app.config,而是直接添加到输出 YourAppName.exe.config 文件。
我有一个 "Classic Desktop" .NET 项目,它引用了来自 NuGet 的 Microsoft.Build
15.1 和来自 .NET SDK / GAC 的 System.IO.Compression
/System.IO.Compression.FileSystem
。
我正在尝试将其升级到 Microsoft.Build
15.3.
Microsoft.Build
15.3 引入了对 System.IO.Compression
4.1.2.0 的依赖。 .NET Framework 中 System.IO.Compression
的版本是 4.0.0.0.
如果我这样编译,我会收到无法解决程序集冲突的警告,但我的代码有效:
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.
将构建日志详细程度设置为详细会产生此输出:
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 Studio17\Enterprise\MSBuild.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 Studio17\Enterprise\MSBuild.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.
我可以通过将 SDK/GAC 对 System.IO.Compression
的引用替换为 NuGet 引用来解决警告。但是,一旦我这样做,当 JIT 到达该方法时,使用 ZipFile
的代码就会崩溃并出现 MissingMethodException
。
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 case 来说明这种行为(查看分支)。
有什么方法可以同时引用 Microsoft.Build
15.3 和 System.IO.Compression
而没有错误、警告或异常吗?
要解决 MSB3277 警告指示的问题(以及缺少方法异常问题)- 您需要将绑定重定向添加到 app.config
文件,以便所有绑定都重定向到最新版本(4.1.在这种情况下为 2.0):
<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>
因此,安装 Microsoft.Build 15.3,从 nuget 安装 System.IO.Compression(这会将您的状态移动到 MissingMethodException 情况下的状态),然后在上面添加重定向,异常将消失,并且一切正常或警告。
评论中建议的替代方法是将所有包更新到最新版本——这也有效,原因相同——绑定重定向。如果您更新所有包然后重建 - 很多重定向将添加到您的配置中,包括 System.IO.Compression 的重定向(但到版本 4.2.0.0)。请注意,这些重定向不会添加到您的 app.config,而是直接添加到输出 YourAppName.exe.config 文件。