C# 具有.Net内核和Kestrel的HTTP/2
我正在尝试在C# 具有.Net内核和Kestrel的HTTP/2,c#,.net-core,asp.net-core-3.1,kestrel-http-server,C#,.net Core,Asp.net Core 3.1,Kestrel Http Server,我正在尝试在ASP.Net Core 3.1中使用Kestrel设置一个Web服务器,以支持HTTP/2。这就是我配置红隼的方式: public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateH
ASP.Net Core 3.1
中使用Kestrel
设置一个Web服务器,以支持HTTP/2
。这就是我配置红隼的方式:
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>();
});
}
}
不幸的是,调用者程序失败,出现此异常IOException:响应过早结束
:
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.
System.Net.Http.HttpRequestException
HResult=0x80131620
Message=发送请求时出错。
Source=System.Net.Http
堆栈跟踪:
在System.Net.Http.HttpConnection.d__53.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()中
在System.Net.Http.HttpConnectionPool.d_u48.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()中
在System.Net.Http.HttpConnectionPool.d_u47.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()中
在System.Net.Http.RedirectHandler.d__4.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()中
在System.Net.Http.HttpClient.d_u70.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在C:\Users\Hans\source\repos\ConsoleApp17\ConsoleApp17\Program.d\u 0.MoveNext()中,第19行
此异常最初是在此调用堆栈中引发的:
System.Net.Http.HttpConnection.FillAsync()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()异常
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskWaiter.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.TaskWaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskWaiter.ValidateEnd(System.Threading.Tasks.Task)
...
[调用堆栈被截断]
内部异常1:
IOException:响应过早结束。
我在这里遗漏了什么?看起来有个问题- 我试过使用Http1,效果不错
listenOptions.Protocols = HttpProtocols.Http1;
客户机中有ReadKey,但服务器中没有。服务器正在终止并处理连接。@jdweng什么???!
Kestrel
服务器位于ASP.NET核心应用程序中。请将端口更改为5001是否有任何设备正在运行?这个答案对你有帮助吗?@Hans:是什么阻止服务器代码终止的?当应用程序关闭时,连接也会关闭。这不是一个grpc
调用。它是一个简单的HTTP请求默认情况下,http2与加密消息一起使用,但有一种方法可以禁用已经禁用的消息
listenOptions.Protocols = HttpProtocols.Http1;