简体   繁体   English

NuGet 包 (.NET Standard 2.x) 破坏 ASP.NET MVC5 应用程序(.NET 完整框架)

[英]NuGet package (.NET Standard 2.x) breaking ASP.NET MVC5 application (.NET full framework)

(updated - see below) (更新 - 见下文)

I have an ASP.NET MVC5 application (multiple Assemblies) all addressing the .NET Full Framework 4.7.1我有一个 ASP.NET MVC5 应用程序(多个程序集),它们都针对 .NET Full Framework 4.7.1

I recently added a (private) NuGet package that we built using .NET Standard 2. From Quickstart: Create and publish a package using Visual Studio (.NET Framework) :我最近添加了我们使用 .NET Standard 2 构建的(私有)NuGet 包。从快速入门:使用 Visual Studio (.NET Framework) 创建和发布包

Unless you have a reason to choose otherwise, .NET Standard is the preferred target for NuGet packages, as it provides compatibility with the widest range of consuming projects.除非您有其他选择的理由,否则 .NET Standard 是 NuGet 包的首选目标,因为它提供与最广泛的使用项目的兼容性。

Since then, things just haven't been the same.....there are a list of errors that I mention below, but I suspect they're all symptoms of the same issue (discussed at the end).从那时起,事情就变得不一样了......我在下面提到了一系列错误,但我怀疑它们都是同一问题的症状(最后讨论)。

Please note : I'm using Visual Studio 15.7.1, and all NuGet packages use the package.config manage format rather than PackageReference.请注意:我使用的是 Visual Studio 15.7.1,并且所有 NuGet 包都使用 package.config 管理格式而不是 PackageReference。

First problem第一个问题

Method not found: 'System.Collections.ObjectModel.Collection`1 System.Web.Http.HttpConfiguration.get_MessageHandlers()'找不到方法:'System.Collections.ObjectModel.Collection`1 System.Web.Http.HttpConfiguration.get_MessageHandlers()'

My guess here is that there's a .NET Standard version of System.Web.Http that doesn't have this method, and my application has chosen to address this rather than the 4.7.1 framework.我的猜测是 System.Web.Http 的 .NET Standard 版本没有这个方法,我的应用程序选择解决这个问题,而不是 4.7.1 框架。

I partially fixed this by adding the following to my web.config file:我通过将以下内容添加到我的 web.config 文件来部分修复了这个问题:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>

I say "partially" fixed it, this fixes the problem when run in my DEV environment (with Visual Studio) but when published to our QA environment, the issue remains.我说“部分”修复了它,这解决了在我的 DEV 环境(使用 Visual Studio)中运行时的问题,但是当发布到我们的 QA 环境时,问题仍然存在。 Any ideas on how to fix this?有想法该怎么解决这个吗?

Note : The MVC project's reference to System.Net.Http exists in the following path (it is NOT a NuGet package):注意:MVC 项目对 System.Net.Http 的引用存在于以下路径中(它不是 NuGet 包):

C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\Microsoft\\Microsoft.NET.Build.Extensions\\net471\\lib\\System.Net.Http.dll C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\Microsoft\\Microsoft.NET.Build.Extensions\\net471\\lib\\System.Net.Http.dll

Second problem Two of the Assemblies are involved with pulling data from various web-sources (WCF, web-api, web-service).第二个问题两个程序集涉及从各种网络源(WCF、web-api、web-service)中提取数据。 When compiling these in RELEASE mode (where I have code analysis etc turned on) I get the following error reported:在 RELEASE 模式下编译这些时(我打开了代码分析等),我报告了以下错误:

An attempt was made to load an assembly with an incorrect format: C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework.NETFramework\\v4.7.1\\Facades\\System.IO.Compression.ZipFile.dll.尝试加载格式不正确的程序集:C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework.NETFramework\\v4.7.1\\Facades\\System.IO.Compression.ZipFile.dll。 {Name of the assembly} {Path to the assembly}\\SGEN {程序集名称} {程序集路径}\\SGEN

I found one post had the following suggestion:我发现一个帖子有以下建议:

Another suggestion I saw (which didn't help me at all), was to disable the project from creating a serialization assembly.我看到的另一个建议(根本没有帮助我)是禁止项目创建序列化程序集。 Follow these steps to do that (it may or may not help, but it's worth a try):请按照以下步骤执行此操作(它可能有帮助也可能无济于事,但值得一试):

  1. In Solution Explorer, right click on the project that has the error and go to Properties.在解决方案资源管理器中,右键单击出现错误的项目并转到属性。
  2. In the Build tab, scroll to the very bottom and change Generate Serialization Assembly to Off在 Build 选项卡中,滚动到最底部并将 Generate Serialization Assembly 更改为 Off

I tried this and yes, it worked for me.我试过这个,是的,它对我有用。 However, these are the two projects that are serializing objects to send to these various web sources, so the last thing I want to be doing is un-optimizing serialization within my application...但是,这是将对象序列化以发送到这些各种 Web 源的两个项目,所以我最不想做的就是在我的应用程序中取消优化序列化......

Any suggestions on what might be wrong, or how I should best proceed?关于可能出现什么问题的任何建议,或者我应该如何最好地进行?

UPDATE 14-May-2018 I found a great article from Scot Hanselman regarding NuGet packages which states: 2018 年 5 月 14 日更新,我从Scot Hanselman找到了一篇关于 NuGet 包的精彩文章,其中指出:

As Oren wisely says:正如奥伦明智地说:

"Using .NET Standard requires you to use PackageReference to eliminate the pain of “lots of packages” as well as properly handle transitive dependencies. While you may be able to use .NET Standard without PackageReference, I wouldn't recommend it." “使用 .NET Standard 要求您使用 PackageReference 来消除“大量包”的痛苦,并正确处理传递依赖项。虽然您可以在没有 PackageReference 的情况下使用 .NET Standard,但我不推荐它。”

However, there's a catch.... In Visual Studio, there is now an easy way to migrate an assembly from the older package.config to PackageReference as detailed in this link , but that article clearly states:但是,有一个问题.... 在 Visual Studio 中,现在有一种简单的方法可以将程序集从旧的 package.config 迁移到 PackageReference,如此链接中所述,但该文章明确指出:

Note that the migrator does not presently support C++, JavaScript, and ASP.NET (.NET Framework) projects.请注意,迁移器目前不支持 C++、JavaScript 和 ASP.NET (.NET Framework) 项目。

And when attempting this, the migration tool duly notified me that for the NuGet packages Microsoft.AspNet.Mvc and Microsoft.AspNet.WebPages:在尝试此操作时,迁移工具会及时通知我对于 NuGet 包 Microsoft.AspNet.Mvc 和 Microsoft.AspNet.WebPages:

"content" assets are not available when the package is installed after the migration迁移后安装包时,“内容”资产不可用

UPDATE 23-May-2018 I found several other people are experiencing a similar issue. 2018 年 5 月 23 日更新我发现其他几个人也遇到了类似的问题。 From GitHub: .NET 4.6.1/.NET Standard 2.0 build with SGen fails #1630 .来自 GitHub: .NET 4.6.1/.NET Standard 2.0 使用 SGen 构建失败 #1630 Their "solution" was again to disable the serialization (see above) which would ruin performance on my application.他们的“解决方案”再次禁用序列化(见上文),这会破坏我的应用程序的性能。

<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies> <GenerateSerializationAssemblies>关闭</GenerateSerializationAssemblies>

Conclusion?结论?

So, am I right to conclude that to consume a .NET Standard 2.x NuGet package in a .NET FullFramework application, I need to use PackageReference rather than package.config and this is currently not possible with AspNet.MVC applications?那么,我是否正确地得出结论,要在 .NET FullFramework 应用程序中使用 .NET Standard 2.x NuGet 包,我需要使用 PackageReference 而不是 package.config 而这目前在 AspNet.MVC 应用程序中是不可能的? In other words, it's not currently possible.换句话说,目前是不可能的。

I've got this problem with .Net Framework 4.8 application and System.Buffers assembly incorrect format我遇到了 .Net Framework 4.8 应用程序和System.Buffers程序集格式不正确的问题

The MarkKharitonov advice has solved the error. MarkKharitonov 的建议已经解决了这个错误。 With Directory.Build.targets file I can built a project without any problem.使用Directory.Build.targets文件,我可以毫无问题地构建项目。

<Project>
  <ItemGroup>
    <ReflectionOnlyAssemblyNames Include="Microsoft.Bcl.AsyncInterfaces"/>
    <ReflectionOnlyAssemblyNames Include="System.Buffers"/>
    <ReflectionOnlyAssemblyNames Include="System.Numerics.Vectors"/>
    <ReflectionOnlyAssemblyNames Include="System.Runtime.CompilerServices.Unsafe"/>
  </ItemGroup>
  <Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <_ReflectionOnlyAssembly_Names Include="@(_ReferencePath_Names)"
                                     Condition="'@(ReflectionOnlyAssemblyNames)' == '@(_ReferencePath_Names)' And '%(Identity)' != ''"/>
    </ItemGroup>
    <ItemGroup>
      <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
      <ReferencePath Remove="@(_ReflectionOnlyAssembly_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
  </Target>
  <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
      <ReferencePath Include="@(_ReflectionOnlyAssembly_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
  </Target>
</Project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM