我的机器/项目详情:
- Windows 版本为 10 Enterprise 1903(操作系统内部版本 18362.295)
- dotnet 核心版本是 2.2.106,目标框架版本是 4.6.2
这是在代码中设置服务器的方式:
IWebHost localWebHost = new WebHostBuilder()
.UseHttpSys()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseApplicationInsights(instrumentationKey)
.UseStartup<Startup>()
.UseUrls("http://+:8644")
.Build();
但是使用 http 2.0 版调用仍会返回 1.1 的响应(下面的 LinqPad 脚本):
async Task Main()
{
using (var httpClient = new HttpClient(new Http2CustomHandler()))
{
var stopwatch = new Stopwatch();
stopwatch.Restart();
var response = await httpClient.SendAsync(new HttpRequestMessage
{
Method = HttpMethod.Get,
// replacing uri with "https://www.google.com" returns http version 2.0
RequestUri = new Uri("http://localhost:8644/api/health", UriKind.Absolute)
});
response.RequestMessage.Version.Dump();
response.Version.Dump();
stopwatch.ElapsedMilliseconds.Dump();
}
}
public class Http2CustomHandler : WinHttpHandler
{
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
request.Version = new Version("2.0");
return base.SendAsync(request, cancellationToken);
}
}
我从 2.0response.RequestMessage.Version
但 1.1 从response.Version
. 在响应中用google的returns 2.0替换本地uri(所以上面的代码是正确的)
HttpSysOptions也没有任何与 http2 相关的参数。有人知道我还缺少什么吗?