.NET Standard 2.x NuGet包致ASP.NET MVC5应用故障排查求助
Hey there, let's work through your two key issues with your .NET Framework 4.7.1 ASP.NET MVC5 app and that custom .NET Standard 2.x NuGet package. I've dealt with similar compatibility headaches before, so here are practical fixes tailored to your scenario:
System.Web.Http.HttpConfiguration.get_MessageHandlers() (Binding Redirect Only Works in Dev) This almost always boils down to mismatched assembly versions between your app, the .NET Standard package, and how binding redirects are deployed to QA. Here's how to fix it:
- Force explicit binding redirects in
web.config
Add a direct redirect forSystem.Web.Httpto the version your app is actually using (check your NuGet packages for the exact version, e.g., 5.2.9.0). This ensures QA picks up the correct version instead of falling back to outdated or conflicting assemblies:<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.9.0" newVersion="5.2.9.0" /> </dependentAssembly> </assemblyBinding> </runtime> - Validate QA deployment settings
Make sure your publish profile isn't stripping or modifying theweb.configduring deployment. In your project's Publish settings, check that "Preserve additional files at destination" is enabled, and verify the deployedweb.configin QA has the binding redirect you added. - Clean up stale assemblies in QA
Old DLLs lingering in the QA server'sbindirectory can cause version conflicts. Delete the entirebinfolder before redeploying to ensure you're starting fresh. - Align
System.Web.Httpversions across your app and package
Ensure your custom .NET Standard package doesn't reference a newer version ofSystem.Web.Httpthan your MVC5 app. If it does, either upgrade your app'sSystem.Web.Httpvia NuGet to match, or adjust your package's dependencies to target a compatible version for .NET Framework 4.7.1.
System.IO.Compression.ZipFile.dll The format error happens because the serializer assembly generator is trying to load a version of ZipFile that's incompatible with .NET Framework 4.7.1. You don't have to disable serializer assemblies entirely—try these fixes:
- Add a binding redirect for
System.IO.Compression.ZipFile
.NET Framework 4.7.1 ships with its own version ofZipFile(usually 4.2.0.0). Redirect any conflicting versions to this built-in one inweb.config:<dependentAssembly> <assemblyIdentity name="System.IO.Compression.ZipFile" publicKeyToken="b77a5c561934e089" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" /> </dependentAssembly> - Tweak serializer assembly generation
Instead of disabling it entirely, set your project to generate serializer assemblies only when needed. Go to your project's Properties > Build > Advanced, then set "Generate serialization assemblies" to "On". This avoids generating unnecessary serializer code while keeping performance gains for types that need it. - Adjust your NuGet package's dependencies
In your custom package's.nuspecfile, add framework-specific dependencies to ensure it pulls the rightZipFileversion for .NET Framework 4.7.1:
This way, when the package is installed in your MVC5 app, it uses the framework-compatible version instead of a newer .NET Standard one.<dependencies> <group targetFramework=".NETStandard2.0"> <dependency id="System.IO.Compression.ZipFile" version="4.3.0" /> </group> <group targetFramework=".NETFramework4.7.1"> <dependency id="System.IO.Compression.ZipFile" version="4.2.0" /> </group> </dependencies>
While the official migration tool doesn't support ASP.NET MVC5, you can manually switch to PackageReference to simplify dependency management (and often fix binding redirect issues automatically):
- Uninstall all NuGet packages from your project via the NuGet Package Manager.
- Edit your
.csprojfile and delete the old<packages.config>reference and any<Import>nodes related to NuGet. - Add
<PackageReference>entries for all your dependencies, like this:<ItemGroup> <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" /> <PackageReference Include="YourCustomNetStandardPackage" Version="1.0.0" /> </ItemGroup> - Save the
.csprojand restore NuGet packages. This will auto-generate binding redirects inweb.configand keep dependencies aligned across environments.
Just make sure to back up your project before making this switch, in case you need to revert.
内容的提问来源于stack exchange,提问作者DrGriff

