5

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

Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
  • 1
    .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. As you mentioned, your application targets version 4.7.2 of the full framework, not .NET Core 2.0 or 2.1 – Panagiotis Kanavos May 22 '18 at 09:37
  • If you want to use it you gotta switch to .NET Core 2.1 and not target .NET Framework 4.7.x anymore. You can still run it on windows and use most of the API, even stuff like registry with the Windows Compatibility Pack. Of course it limits you to windows as hosting platform but you benefit from the improvements of .NET Core – Tseng May 22 '18 at 14:52
  • @PanagiotisKanavos, please check MS's doc linked in my question, the target framework has to be set to .NET Framework when hosting ASP.NET Core in Windows services. And it works well targeting .NET 4.7.1 when I use .NET Core 2.0 – Mr.Wang from Next Door May 23 '18 at 02:11
  • @Tseng, I am following Microsoft's [doc](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.0&tabs=aspnetcore2x) which clearly explained targetFramework has to be .NET framework for my ASP.NET Core application. – Mr.Wang from Next Door May 23 '18 at 02:16
  • @Mr.WangfromNextDoor that document isn't about .NET Core, the runtime. I already use SocketHttpHandler on .NET Core 2.1 RC in console applications. You can't use it on .NET Framework 4.7 at all, whether you use the ASP.NET Core stack or not. It's not part of the ASP.NET Core stack. It's part of the .NET Core BCL and you aren't running on that runtime. If you want to use SocketHttpHandler you'll have to create a new .NET Core 2.1 project with the ASP.NET Core template – Panagiotis Kanavos May 23 '18 at 07:37
  • @Mr.WangfromNextDoor anyway, you *don't* have to target the full framework to create an ASP.NET Core Web Application, in fact that's the least popular option. If you need to target the full framework for other reasons, eg because you use a library that can't run on .NET Core, you won't be able to use SocketsHttpHandler – Panagiotis Kanavos May 23 '18 at 07:39

1 Answers1

4

.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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236