以.NET 4.7.2为目标时,引用System.Net.Http的正确方法是什么? [英] What is the right way to reference System.Net.Http when targeting .NET 4.7.2?

查看:64
本文介绍了以.NET 4.7.2为目标时,引用System.Net.Http的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a solution which contains a few web applications and so AspNetCompiler task is running during the build. Also given:

  • VS 2017
  • 通过/err开关在命令行上调用
  • msbuild,即将警告升级为错误
  • VS 2017
  • msbuild is invoked on the command line with the /err switch, i.e. warnings are promoted to errors

从头开始构建代码时,出现以下错误(可以提升为警告,我不在乎):

When building our code from scratch I get the following errors (could be promoted warnings, I do not care):

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]

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

All our projects reference System.Net.Http like this:

<Reference Include="System.Net.Http" />

没有项目在其app.config或web.config文件中对此装配件进行绑定重定向.

No project does binding redirects for this assembly in its app.config or web.config file.

所有项目都针对.NET Framework 4.7.2,但某些第三方依赖项是.NET Standard.

All the projects target the .NET framework 4.7.2, but some 3rd party dependencies are .NET Standard.

无论如何,我已经搜索了Internet,并且:

Anyway, I have searched the Internet and:

  1. 有人声称只能从NuGet引用System.Net.Http,有人则相反.
  2. 有人声称绑定重定向是必要的,而另一些人则相反.
  3. 有人说问题已通过.NET Framework 4.7.2解决(显然没有)

我也知道:

  • GAC包含System.Net.Http.dll,其汇编版本为 4.0.0.0 ,文件版本为 4.7.3190.0,由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,由NET472REL1 构建.
  • The GAC contains System.Net.Http.dll with the Assembly Version of 4.0.0.0 and the File Version of 4.7.3190.0 built by: NET472REL1LAST_C. From which I conclude it correspond to .NET 4.7.2
  • On the other hand, during the compilation, msbuild resolves the reference to c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll, which has the Assembly Version of 4.2.0.0 and the File Version of 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引入了这种混乱情况.

Now I am confused. The file version of System.Net.Http.dll from the Reference Assemblies suggests it is older than that from the GAC, but its Assembly Version is 4.2.0.0 ? How will this dependency be resolved at run-time on a machine that has the version 4.0.0.0 in the GAC and nothing else? Checking the Reference Assemblies for older .NET versions (like 3.7.1 and below) shows System.Net.Http.dll with the Assembly Version of 4.0.0.0, so this mess is introduced with the .NET framework 4.7.2

那么,如何使该死的东西干净地编译?另外,我不明白为什么只有AspNetCompiler抱怨.一个人怎么根本解决它?二进制日志似乎一点也不帮助AspNetCompiler错误.

So, how do I make the damn thing compile cleanly? Also, I do not understand how come only AspNetCompiler complains. How does one troubleshoot it at all? The binary log does not seem to help with AspNetCompiler errors one bit.

推荐答案

到目前为止,我仅找到一种使这项工作可行的方法.我们必须转到.net framework 4.7.2来满足Chromium SameSite Cookie的要求.似乎有几个站点对此进行了记录,但是由于NuGet依赖性问题列表很长,所以我们无法使用Microsoft.Owin4.x.下面的一些信息是针对SameSite修复的,但是我在.net Framework 4.7.2中的System.Net.Http程序集上苦苦挣扎,因此也在下面.

I have only found 1 way to make this work so far. We had to go to .net framework 4.7.2 to take care of the Chromium SameSite cookie requirements. There seem to be several sites documenting this, but we could not go to Microsoft.Owin 4.x due to a long list of NuGet dependency issues. Some of the info below is for the SameSite fix, but I struggled a lot with the System.Net.Http assembly in .net framework 4.7.2 so that is below too.

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

I found if you take your MVC and Web.Api projects to .net framework 4.7.2, then make sure all your projects that use System.Net.Http reference the GAC 4.2.0.0 version. I have class library projects that also run Web.api controllers. Those I also installed the NuGet System.Net.Http along side the GAC version. You may have to edit your project file to reference both. The NuGet assembly reference in the project file should include an ExcludeAssets element. Example Exclude Assets I ended up converting my non-main project web.api class library projects to use the PackageReference instead of packages.config. I think this is required to use the excludeAssets.

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

I also used the nuget package manager console to re-install all nuget packages to target framework 4.7.2. Re-install NuGet packages to re-target framework 4.7.2

您的主项目web.config应该具有为System.Net.Http定义的运行时相关的Assembly部分.需要排除它,以便自动选择它.我所做的其他类库项目包括对System.Net.Http版本4.2.0.0的dependentAssembly引用.我还向我的Views web.config和我的Areas中的所有Views web.config文件添加了程序集定义和运行时dependentAssembly.我不确定这有什么必要.

Your main project web.config should NOT have a runtime dependentAssembly section defined for System.Net.Http. It needs to be excluded so it is selected automatically. The other class library projects I did include the dependentAssembly reference to System.Net.Http version 4.2.0.0. I also added the assembly definition and runtime dependentAssembly to my Views web.config and all my Views web.config files within my Areas. I'm not sure how necessary this is.

<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>

我使用此站点来帮助编写cookie重写器.我发现这只照顾system.web cookie,而不照顾owin cookie.同一网站的Cookie重写器

I used this site to help write a cookie rewriter. I found that this only takes care of system.web cookies and not OWIN cookies. Cookie rewriter for same site

我没有访问4.x OWIN修补程序以将SameSite属性添加到OWIN cookie的方法,但是我确实找到了一种方法,可以通过将SystemWebCookieManager添加到OWIN选项来使系统自动为您完成此操作. SameSiteCookieManager

I didn't have access to the 4.x OWIN fix to add the SameSite property to the OWIN cookies, but I did find a way that the system will do it for you automatically by adding the SystemWebCookieManager to the OWIN options. SameSiteCookieManager

这篇关于以.NET 4.7.2为目标时,引用System.Net.Http的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆