简体   繁体   English

HTTP/2 与 .Net Core 和 Kestrel

[英]HTTP/2 with .Net Core and Kestrel

I'm trying to setup a Web Server with Kestrel in ASP.Net Core 3.1 to support HTTP/2 .我正在尝试在ASP.Net Core 3.1中使用Kestrel设置 Web 服务器以支持HTTP/2 So this is how I configure Kestrel:所以这就是我配置 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:这就是我通过在控制台应用程序中使用先前的HTTP/2知识发出请求来测试它的方式:

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 :不幸的是,调用程序失败并出现此异常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:\Users\Hans\source\repos\ConsoleApp17\ConsoleApp17\Program.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?我在这里想念什么?

looks like there is an issue - https://github.com/dotnet/aspnetcore/issues/27536#issuecomment-731932924 I tried with Http1 worked ok看起来有问题 - https://github.com/dotnet/aspnetcore/issues/27536#issuecomment-731932924我尝试使用 Http1 工作正常

listenOptions.Protocols = HttpProtocols.Http1;

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

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