Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.Net.Http.SocketsHttpHandler in .NET Core 2.1.0-rc1-final

.NET Core 2.1 comes with a great improvement on System.Net.HttpClient

For HttpClient, we built a new from-the-ground-up managed HttpClientHandler called SocketHttpHandler. As you can likely guess, it’s a C# implementation of HttpClient based on .NET sockets and Span.

My application is an ASP.NET Core application hosted as windows service, according to Microsoft this doc , the project is a .NET Core based but its TargetFramework is .NET framework.

The application works well with .NET Core 2.0 targeting .NET framework 4.7.1.

Recently Microsoft released Microsoft.AspNetCore 2.1.0-rc1-final which is ready for production use. So I tried to upgrade to this version.

so I upgraded TargetFramework to 4.7.2 and upgrade the reference as bellow.

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

Then it stops working and compiler raises error like The type or namespace name 'HttpClient' could not be found

I am aware of HttpClient exists in NuGet System.Net.Http assembly.

However its latest version is 4.3.3 from 8 months ago, which seems to me it does not contain the implementation of SocketHttpHandler.

So my question is: How could I use the latest HttpClient from .NET Core 2.1 targeting .NET framework 4.7.2 ?

like image 847
Mr.Wang from Next Door Avatar asked Oct 17 '22 18:10

Mr.Wang from Next Door


1 Answers

.NET Core is not the same as ASP.NET Core. .NET Core is the underlying runtime. ASP.NET Core is a framework that can run on top of .NET Core or the full .NET Framework runtime. As you mentioned, your application targets version 4.7.1 of the full framework, not .NET Core 2.0 or 2.1

The new HttpClient and SocketsHttpHandler are part of .NET Core 2.1. They are included in the System.NET.Http assembly. You can use them in any application that targets that runtime, even desktop applications. I already do so without any references to ASP.NET Core assemblies or packages. All I added was a reference to System.Net.Http.

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

And a sample call :

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!");
    }
}

Your project will have to target that specific runtime, not even 2.0 if you want to use them. You'll also need to install the .NET Core 2.1 RC 1 SDK to create and build projects that target .NET Core 2.1. You'll also have to install the new runtime to the production severs.

Once you do that, you can create a .NET Core project from the command line with dotnet new and the name of the template, eg :

dotnet new razor
dotnet new mvc
dotnet new angular

Or through Visual Studio, from the Visual C# > .NET Core category and selecting the ASP.NET Core Web Application template.

You can also create it from the Web > ASP.NET Core Web Application template provided you select .NET Core as the runtime and ASP.NET Core 2.1 as the target.

This class is only available if you target .NET Core 2.1. If I change the target to netcoreapp2.0 the compiler will complain that SocketsHttpHandler doesn't exist.

Right now there is no documentation about SocketsHttpHandler except for a couple of blog posts and the source code itself

like image 55
Panagiotis Kanavos Avatar answered Oct 21 '22 09:10

Panagiotis Kanavos