System.Net.Http 与 Microsoft.Net.Http [英] System.Net.Http vs Microsoft.Net.Http

查看:33
本文介绍了System.Net.Http 与 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 包(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屋!

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