简体   繁体   English

System.Net.Http 与 Microsoft.Net.Http

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

I am using ASP.NET Core.我正在使用 ASP.NET 核心。 I want to use HttpClient but I noticed that there are two NuGet packages being offered.我想使用HttpClient但我注意到提供了两个 NuGet 包。 Which one do I use?我使用哪一种?

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包( 2.0的)是旧包,根据描述,这些包已被弃用,取而代之的是Microsoft.Http.Net

Legacy package, System.Net.Http is now included in the 'Microsoft.Net.Http' package.旧包 System.Net.Http 现在包含在“Microsoft.Net.Http”包中。

They exist to provide the HttpClient in previous .NET versions and Portable Class libraries.它们的存在是为了在以前的 .NET 版本和可移植类库中提供HttpClient You should use Microsoft.Net.Http in that case.在这种情况下,您应该使用Microsoft.Net.Http

Since you're using .NET Core, you should use the latest System.Net.Http package (eg. 4.3.3).由于您使用的是 .NET Core,您应该使用最新的System.Net.Http包(例如 4.3.3)。

Updated for csproj为 csproj 更新

As of .NET Standard 2.0, the System.Net.HttpClient package is already included and available when you target netstandard2.0 .从 .NET Standard 2.0 开始, System.Net.HttpClient包已经包含在内,并且在您定位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:如果出于某种原因,您仍然希望为完整的 .NET 和 .NET Core 引用它,您可以将其添加到您的 csproj 文件中:

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

If you're using project.json如果您使用的是 project.json

If your project.json targets both full .NET and .NET Core, you have to add the System.Net.Http assembly to the frameworkAssemblies element.如果您的 project.json 面向完整的 .NET 和 .NET Core,则必须将System.Net.Http程序集添加到frameworkAssemblies元素。 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
    }
  }
}

For anyone interested in more background to this, Immo Landwerth (Program manager on .NET at Microsoft) tweeted about this:对于任何对此更多背景感兴趣的人, Immo Landwerth (Microsoft .NET 项目经理)在推特上发布了以下内容:

"HttpClient started out as a NuGet package (out-of-band) and was added to the .NET Framework in 4.5 as well (in-box). “HttpClient 最初是一个 NuGet 包(带外),并在 4.5 中(内置)添加到 .NET Framework 中。

With .NET Core/.NET Standard we originally tried to model the .NET platform as a set of packages where being in-box vs. out-of-band no longer mattered.对于 .NET Core/.NET Standard,我们最初尝试将 .NET 平台建模为一组包,其中盒内与带外不再重要。 However, this was messier and more complicated than we anticipated.然而,这比我们预期的更加混乱和复杂。

As a result, we largely abandoned the idea of modeling the .NET platform as a NuGet graph with Core/Standard 2.0.因此,我们基本上放弃了将 .NET 平台建模为带有 Core/Standard 2.0 的 NuGet 图的想法。

The general answer is:一般的答案是:

With .NET Core 2.0 and .NET Standard 2.0 you shouldn't need to reference the SystemNetHttpClient NuGet package at all.使用 .NET Core 2.0 和 .NET Standard 2.0,您根本不需要引用 SystemNetHttpClient NuGet 包。 It might get pulled from 1.x dependencies though.不过,它可能会从 1.x 依赖项中提取出来。

Same goes for .NET Framework: if you target 4.5 and up, you should generally use the in-box version instead of the NuGet package. .NET Framework 也是如此:如果你的目标是 4.5 及更高版本,你通常应该使用内置版本而不是 NuGet 包。 Again, you might end up pulling it in for .NET Standard 1.x and PCL dependencies, but code written directly against .NET Framework shouldn't use it.同样,您最终可能会将其用于 .NET Standard 1.x 和 PCL 依赖项,但直接针对 .NET Framework 编写的代码不应使用它。

So why does the package still exist/why do we still update it?那么为什么包仍然存在/为什么我们仍然更新它? Simply because we want to make existing code work that took a dependency on it.仅仅是因为我们想让依赖于它的现有代码工作。 However, as you discovered that isn't smooth sailing on .NET Framework.但是,正如您发现的那样,在 .NET Framework 上并非一帆风顺。

The intended model for the legacy package is: if you consume the package from .NET Framework 4.5+, .NET Core 2+, .NET Standard 2+ the package only forwards to the platform provided implementation as opposed to bring it's own version.旧包的预期模型是:如果您使用来自 .NET Framework 4.5+、.NET Core 2+、.NET Standard 2+ 的包,该包仅转发到平台提供的实现,而不是带来它自己的版本。

That's not what actually happens in all cases though: the HTTP Client package will (partially) replace in-box components on .NET Framework which happen to work for some customers and fails for others.但这并不是在所有情况下实际发生的情况:HTTP 客户端包将(部分)替换 .NET Framework 上的内置组件,这些组件碰巧适用于某些客户,而对其他客户却失败了。 Thus, we cannot easily fix the issue now.因此,我们现在不能轻易解决这个问题。

On top of that we have the usual binding issues with the .NET Framework so this only really works well if you add binding redirects.最重要的是,我们有 .NET Framework 的常见绑定问题,因此只有添加绑定重定向才能真正有效地工作。 Yay!好极了!

So, as a library author my recommendation is to avoid taking a dependency on this package and prefer the in-box versions in .NET Framework 4.5, .NET Core 2.0 and .NET Standard 2.0."因此,作为库作者,我的建议是避免依赖此包,而更喜欢 .NET Framework 4.5、.NET Core 2.0 和 .NET Standard 2.0 中的内置版本。”

https://twitter.com/terrajobst/status/997262020108926976 https://twitter.com/terrajobst/status/997262020108926976

Microsoft.Net.Http requires additional Microsoft.Bcl dependencies. Microsoft.Net.Http需要额外的Microsoft.Bcl依赖项。

For that, if you are only targeted .NET Framework or .NET Core, System.Net.Http is good to go.为此,如果您只针对 .NET Framework 或 .NET Core,那么System.Net.Http是不错的选择。 Otherwise, Microsoft.Net.Http would be better choice as it could be the next generation.否则, Microsoft.Net.Http将是更好的选择,因为它可能是下一代。

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

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