AnswerBun.com

HTTP/2 with .Net Core and Kestrel

Stack Overflow Asked by Hans on December 14, 2020

I’m trying to setup a Web Server with Kestrel in ASP.Net Core 3.1 to support HTTP/2. So this is how I configure Kestrel:

public class Program
{
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseKestrel(options =>
                {
                    options.ListenLocalhost(8080, listenOptions =>
                    {
                        listenOptions.Protocols = HttpProtocols.Http2;
                    });
                })
                .UseStartup<Startup>();
            });
        }
}

And this is how I test it by making a request with prior HTTP/2 knowledge in a console application:

class Program
{
        static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            using var client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:8080"),
                DefaultRequestVersion = new Version(2, 0)
            };
            var req = new HttpRequestMessage(HttpMethod.Get, "/");
            var res = await client.SendAsync(req);
            var version = res.Version;
            Console.WriteLine(version);
            Console.ReadKey();
        }
    }
}

Unfortunately, the caller program fails with this exception IOException: The response ended prematurely:


System.Net.Http.HttpRequestException
  HResult=0x80131620
  Message=An error occurred while sending the request.
  Source=System.Net.Http
  StackTrace:
   at System.Net.Http.HttpConnection.<SendAsyncCore>d__53.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpConnectionPool.<SendWithNtConnectionAuthAsync>d__48.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpConnectionPool.<SendWithRetryAsync>d__47.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__70.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ConsoleApp17.Program.<Main>d__0.MoveNext() in C:UsersHanssourcereposConsoleApp17ConsoleApp17Program.cs:line 19

  This exception was originally thrown at this call stack:
    System.Net.Http.HttpConnection.FillAsync()
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
    System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(bool)
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task)
    ...
    [Call Stack Truncated]

Inner Exception 1:
IOException: The response ended prematurely.

What am I missing here?

One Answer

looks like there is an issue - https://github.com/dotnet/aspnetcore/issues/27536#issuecomment-731932924 I tried with Http1 worked ok

listenOptions.Protocols = HttpProtocols.Http1;

Answered by coder_b on December 14, 2020

Add your own answers!

Related Questions

How to insert multiple images in the Django blog app?

1  Asked on November 29, 2021 by waw-waw

 

Filter out Certain Words and Match only EXACT Words

1  Asked on November 27, 2021 by askmeaquestion1234

         

soup.find returning “none” only sometimes?

2  Asked on November 27, 2021 by c120

   

Raise float into the power – print formula

1  Asked on November 27, 2021 by markl

   

Conditional Window Functions

1  Asked on November 27, 2021 by caerus

   

unable to load font awesome icons in html

1  Asked on November 27, 2021 by vasilis-skentos

     

Why the method is returning padEnd as undefined?

1  Asked on November 27, 2021 by dev101

   

React js .bind(this) in hooks

1  Asked on November 27, 2021 by soukaina-benchekroun

   

How to get a specific name from another array by its id?

1  Asked on November 27, 2021 by ahmed-eid

   

Insertion Sort on a String Array

1  Asked on November 27, 2021 by 101001

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP