问题
I am going mad here, and I am hoping that it's something that I've simply overlooked.
I am experiencing intermittent FileLoadExceptions
, which are showing up after deployments even if the code change between deployments doesn't change any assembly references.
Looking at the most recent example of this, I am seeing a FileLoadException
due to System.IO.Compression
, version 4.2.0.0
not being found.
In all cases, we are referencing the System.IO.Compression
nuget package, version 4.3.0
.
Looking at two projects in our solution, I noticed something very strange.ProjectA
references ProjectB
.
ProjectA
has in its packages.config
file the following reference:
<package id="System.IO.Compression" version="4.3.0" targetFramework="net462" />
ProjectB
has in its package.config
file the following reference:
<package id="System.IO.Compression" version="4.3.0" targetFramework="net462" />
When I have a look into the *.csproj
files, I see this:
ProjectA
:
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
</Reference>`
ProjectB
:
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
</Reference>`
Great, we are pointing to the same assembly on disk in both cases.
Yet, when I look at the referenced file in the Solution Explorer, I see this:
ProjectA
:
The above is referencing a file that's in C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.IO.Compression.dll
, and more importantly has a version of 4.2.0.0
, rather than the version that's in the nuget packages folder.
ProjectB
:
The above correctly points to the nuget packages version of the assembly, which is in fact 4.1.2.0
.
To reiterate, ProjectA
, which references ProjectB
, and both have a a binding redirect that's doing the following:
<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>`
So my question is why is Visual Studio pulling down a version of System.IO.Compression
from a location that is not referenced (directly) by any of our projects? And, what can I do to fix this?
Further, while locally I am using the (current) RC version of Visual Studio 2019, our build agents (Azure DevOps Pipelines) are using Visual Studio 2017.
At runtime, we find the aforementioned exception logged, and our processing that creates a ZIP file fails.
Update
Further to the above, I've done some additional digging, and found a binding redirect that points to the 4.2.0.0
version of this assembly. I've dropped that manually down to 4.1.2.0
, and am deploying again to our test environment with some additional health checks to see how we go.
Still remaining to understand how we would have gotten into this state, and why the discrepancies with what the csproj
is pointing to vs. what is seen in the Solution Explorer.
回答1:
Projects with same nuget package refrencing different version of assembly
This is a known issue when we build a .NET Framework 4.6.x app.
That because:
This is due to the injected support for NETStandard 2.0. We inject new assemblies into NET 4.6.1 and later desktop projects in order to add support for netstandard2.0. We do this in targets now instead of packages because its no longer a requirement to reference a package to build a netstandard library. This injection happens whenever we see a netstandard1.5 or greater library referenced (see dotnet/sdk#1386).
To resolve this issue, we could add binding redirect to those reference to use standard references to System.IO.Compression
and not bring in any Nuget package for System.IO.Compression
. If you still want to use the reference System.IO.Compression
from nuget package, you can delete System.IO.Compression
from the MSBuild tooling.
Check more detail info from the Github:
https://github.com/dotnet/corefx/issues/25773
Hope this helps.
来源:https://stackoverflow.com/questions/55037565/projects-with-same-nuget-package-referencing-different-version-of-assembly