面向 .NET 4.7.2 时引用 System.Net.Http 的正确方法是什么?

mar*_*ark 9 .net visual-studio-2017

我有一个包含一些 Web 应用程序的解决方案,因此 AspNetCompiler 任务在构建期间运行。还给出:

  • VS 2017
  • msbuild 在命令行上使用 /err 开关调用,即警告被提升为错误

从头开始构建我们的代码时,我收到以下错误(可能是警告,我不在乎):

ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.Web.SystemAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.Web.SystemAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: RecruitingCommon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: RecruitingCommon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.Web.Reporting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.Web.Reporting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.Web.ClientAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.Web.ClientAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: DataCapturing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: DataCapturing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\CandidatePortal\CandidatePortal.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.BusinessAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a; System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\SSOLauncher\SSOLauncher.csproj]
ASPNETCOMPILER : error : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Xyz.BusinessAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3dac0a0872f0a93. The dependencies are: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a; System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly. [C:\Xyz\tip\UI\SSOLauncher\SSOLauncher.csproj]
Run Code Online (Sandbox Code Playgroud)

我们所有的项目都像这样引用 System.Net.Http:

<Reference Include="System.Net.Http" />
Run Code Online (Sandbox Code Playgroud)

没有项目在其 app.config 或 web.config 文件中为此程序集绑定重定向。

所有项目都针对 .NET framework 4.7.2,但一些 3rd 方依赖项是 .NET Standard。

无论如何,我已经在互联网上搜索过:

  1. 有人声称引用 System.Net.Http 应该只从 NuGet 完成,有人说相反。
  2. 有些人声称绑定重定向是必要的,其他人则相反。
  3. 有人说 .NET Framework 4.7.2 解决了这个问题(显然不是)

我也知道:

  • GAC 包含 System.Net.Http.dll,其程序集4.0.0.0版本为4.7.3190.0 built by: NET472REL1LAST_C. 从中我得出结论它对应于 .NET 4.7.2
  • 另一方面,在编译期间,msbuild 会解析对 的引用c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll,该引用的程序集版本为4.2.0.0,文件版本为4.7.3062.0 built by: NET472REL1

现在我很困惑。来自参考程序集的 System.Net.Http.dll 的文件版本表明它比来自 GAC 的文件版本旧,但它的程序集版本是 4.2.0.0 ?在 GAC 版本为 4.0.0.0 而没有其他任何内容的机器上,如何在运行时解决此依赖关系?检查旧 .NET 版本(如 3.7.1 及更低版本)的参考程序集显示 System.Net.Http.dll 的程序集版本为 4.0.0.0,因此 .NET 框架 4.7.2 引入了这种混乱

那么,我如何让该死的东西干净利落地编译?另外,我不明白为什么只有 AspNetCompiler 抱怨。如何解决它呢?二进制日志似乎对 AspNetCompiler 错误一点帮助都没有。

gil*_*079 4

到目前为止我只找到了一种方法来完成这项工作。我们必须使用 .net Framework 4.7.2 来满足 Chromium SameSite cookie 要求。似乎有几个站点记录了这一点,但由于一长串 NuGet 依赖性问题,我们无法访问 Microsoft.Owin 4.x。下面的一些信息是针对 SameSite 修复的,但我在 .net Framework 4.7.2 中的 System.Net.Http 程序集上遇到了很多困难,因此也如下。

我发现如果您将 MVC 和 Web.Api 项目迁移到 .net Framework 4.7.2,那么请确保所有使用 System.Net.Http 的项目都引用 GAC 4.2.0.0 版本。我有也运行 Web.api 控制器的类库项目。我还安装了 NuGet System.Net.Http 以及 GAC 版本。您可能需要编辑项目文件才能引用两者。项目文件中的 NuGet 程序集引用应包含 ExcludeAssets 元素。排除资产示例 我最终将非主项目 web.api 类库项目转换为使用 PackageReference 而不是 packages.config。我认为这是使用排除资产所必需的。

我还使用 nuget 包管理器控制台将所有 nuget 包重新安装到目标框架 4.7.2。重新安装 NuGet 包以重新定位框架 4.7.2

您的主项目 web.config 不应System.Net.Http 定义运行时相关的Assembly 部分。需要排除它,因此会自动选择它。我所做的其他类库项目包括对 System.Net.Http 版本 4.2.0.0 的 dependentAssembly 引用。我还将程序集定义和运行时相关程序集添加到我的视图 web.config 以及我的区域中的所有视图 web.config 文件中。我不确定这有多么必要。

<system.web>
    <compilation>
        <assemblies>
            <add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </assemblies>
    </compilation>
</system.web>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <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>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>
Run Code Online (Sandbox Code Playgroud)

我使用这个网站来帮助编写 cookie 重写器。我发现这仅处理 system.web cookie,而不处理 OWIN cookie。同一站点的 Cookie 重写器

我无法访问 4.x OWIN 修复程序以将 SameSite 属性添加到 OWIN cookie,但我确实找到了一种方法,系统可以通过将 SystemWebCookieManager 添加到 OWIN 选项来自动为您完成此操作。SameSiteCookie管理器