MissingMethodException on link to Microsoft.Build and System.IO.Compression

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

I am trying to upgrade it to Microsoft.Build 15.3.

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

If I compile this, I get a warning about the inability to resolve assembly conflicts, but my code works:

warning MSB3277: Conflicts were detected between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when detailed log information is specified.

Configuring a multi-line build log for verbose output of this result:

 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. 

I can resolve this warning by replacing the SDK / GAC link on System.IO.Compression NuGet link. However, as soon as I do this, code using ZipFile crashes with MissingMethodException when the JIT gets this method.

Unhandled exception: System.MissingMethodException: method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead (System.String)'. in CompressionMissingMethod.Program.Main (String [] args)

I put together a very minimal playback example that illustrates this behavior (check the branches).

Is there a way to refer to both Microsoft.Build 15.3 and System.IO.Compression without errors, warnings or exceptions?

+5
source share
1 answer

To fix the problem indicated in warning MSB3277 (as well as the problem with the absence of an excluded method), you need to add the binding redirection to your app.config file so that all the bindings are redirected to the latest 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> 

So, install Microsoft.Build 15.3, install System.IO.Compression from nuget (which puts your state in the MissingMethodException state of affairs), then add the redirection above and the exception will disappear and everything will work without exceptions or warnings.

An alternative suggested in the comments is updating all packages to the latest versions - this also works for the same reason - redirect binding. If you upgrade all packages, then rebuild - many redirects will be added to your configuration, including redirection for System.IO.Compression (but up to version 4.2.0.0). Please note that these redirects will not be added to your app.config, but directly to the output file YourAppName.exe.config.

+6
source

Source: https://habr.com/ru/post/1273039/


All Articles