System.Net.Http与Microsoft.Net.Http [英] System.Net.Http vs Microsoft.Net.Http
问题描述
我正在使用ASP.NET Core.我想使用HttpClient
,但是我注意到提供了两个NuGet软件包.我该使用哪一个?
I am using ASP.NET Core. I want to use HttpClient
but I noticed that there are two NuGet packages being offered. Which one do I use?
- System.Net.Http
- Microsoft.Net.Http
推荐答案
取决于版本.旧的System.Net.Http
软件包( 2.0 的软件包)是旧版根据以下说明不推荐使用 Microsoft.Http.Net
的软件包:>
Depends on the version. The old System.Net.Http
packages (the 2.0 ones) are legacy packages which are deprecated in favor of Microsoft.Http.Net
according to the description:
旧版软件包System.Net.Http现在包含在 "Microsoft.Net.Http"包.
Legacy package, System.Net.Http is now included in the 'Microsoft.Net.Http' package.
它们存在是为了在.NET以前的版本和可移植类库中提供HttpClient
.在这种情况下,您应该使用Microsoft.Net.Http
.
They exist to provide the HttpClient
in previous .NET versions and Portable Class libraries. You should use Microsoft.Net.Http
in that case.
由于使用的是.NET Core,因此应使用最新的 System.Net.Http
包(例如4.3.3).
Since you're using .NET Core, you should use the latest System.Net.Http
package (eg. 4.3.3).
已针对csproj更新
从.NET Standard 2.0开始,System.Net.HttpClient
程序包已经包含并且在您定位netstandard2.0
时可用.如果出于某些原因仍要为完整的.NET和.NET Core都引用它,则可以将其添加到csproj文件中:
As of .NET Standard 2.0, the System.Net.HttpClient
package is already included and available when you target netstandard2.0
. If for some reason you still want to reference it for both full .NET and .NET Core you can add this to your csproj file:
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<!-- // HttpClient for full .NET -->
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<!-- // HttpClient for .NET Core -->
<PackageReference Include="System.Net.Http" Version="4.3.3" />
</ItemGroup>
如果您使用的是project.json
如果project.json同时面向完整的.NET和.NET Core,则必须将System.Net.Http
程序集添加到frameworkAssemblies
元素.例如:
If your project.json targets both full .NET and .NET Core, you have to add the System.Net.Http
assembly to the frameworkAssemblies
element. For example:
"frameworks": {
"net451": {
"frameworkAssemblies": {
"System.Net.Http": "4.0.0.0" // HttpClient for full .NET
}
},
"netstandard1.3": {
"dependencies": {
"System.Net.Http": "4.1.0", // HttpClient for .NET Core
}
}
}
这篇关于System.Net.Http与Microsoft.Net.Http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!