在.NET Core 2.1.0-rc1-final中使用System.Net.Http.SocketsHttpHandler

时间:2018-05-22 06:10:06

标签: asp.net-core .net-core asp.net-core-2.0 dotnet-httpclient system.net

.NET Core 2.1在System.Net.HttpClient

上附带了很棒的improvement
  

对于HttpClient,我们构建了一个全新的on-the--up-managed   HttpClientHandler名为SocketHttpHandler。你可能会猜到,   它是基于.NET套接字和HttpClient的C#实现   跨度。

我的应用程序是作为Windows服务托管的ASP.NET Core应用程序,根据Microsoft this doc,该项目是基于.NET Core的,但其TargetFramework是.NET框架。

该应用程序适用于面向.NET framework 4.7.1的.NET Core 2.0。

最近,微软发布了Microsoft.AspNetCore 2.1.0-rc1-final,可供生产使用。所以我试着升级到这个版本。

所以我将TargetFramework升级到4.7.2,然后升级参考文件。

<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0-rc1-final" /> <PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="2.1.0-rc1-final" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.1.0-rc1-final" /> <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.0-rc1-final" /> <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.0-rc1-final" /> <PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.1.0-rc1-final" />

然后它停止工作,编译器引发错误,如无法找到类型或命名空间名称'HttpClient'

我知道NuGet HttpClient汇编中存在System.Net.Http

然而,它的最新版本是从8个月前开始的4.3.3,在我看来它不包含SocketHttpHandler的实现。

所以我的问题是:我如何使用面向.NET framework 4.7.2的.NET Core 2.1中的最新HttpClient?

1 个答案:

答案 0 :(得分:3)

.NET Core与ASP.NET Core不同。 .NET Core是底层运行时。 ASP.NET Core是一个可以在.NET Core或完整的.NET Framework运行时之上运行的框架。如您所述,您的应用程序的目标是完整框架的4.7.1版本,而不是.NET Core 2.0或2.1

新的HttpClient和SocketsHttpHandler是.NET Core 2.1 的一部分。它们包含在System.NET.Http程序集中。您可以在任何面向运行时的应用程序中使用它们,甚至是桌面应用程序。我已经这样做了,没有任何对ASP.NET Core程序集或包的引用。我添加的只是对System.Net.Http。

的引用

我的csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="System.Net.Http">
      <HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.1.0-rc1\ref\netcoreapp2.1\System.Net.Http.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

示例电话:

using System;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var handler = new SocketsHttpHandler
        {
            MaxConnectionsPerServer=100,
            AutomaticDecompression=DecompressionMethods.GZip|DecompressionMethods.Deflate
        };
        var client = new HttpClient(handler);
        var result = await client.GetStringAsync(someUrl);
        Console.WriteLine("Hello World!");
    }
}

您的项目必须针对特定的运行时,如果您想使用它们,甚至不是2.0。您还需要安装.NET Core 2.1 RC 1 SDK来创建和构建面向.NET Core 2.1的项目。您还必须将新运行时安装到生产服务器。

执行此操作后,您可以从命令行使用dotnet new和模板名称创建.NET Core项目,例如:

dotnet new razor
dotnet new mvc
dotnet new angular

或通过Visual Studio,从Visual C# > .NET Core类别中选择ASP.NET Core Web Application模板。

您也可以从Web > ASP.NET Core Web Application模板创建它,前提是您选择.NET Core作为运行时,ASP.NET Core 2.1作为目标。

如果您的目标是.NET Core 2.1 ,则此类可用。如果我将目标更改为netcoreapp2.0,编译器会抱怨SocketsHttpHandler不存在。

目前没有关于SocketsHttpHandler的文档,除了几篇博文和the source code itself