How to enable ALPN in .Net 5 projects
See original GitHub issueIn continuation to this topic https://github.com/grpc/grpc-dotnet/issues/1252
The problem is still being reproduced. It is not clear how it is determined whether or not there will be an ALPN section in the ClientHello message.
I created two test projects - a client project and a server project. Server: GrpcService2.zip
public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
});
});
}
}
Client: ConsoleApp8.zip
static void Main()
{
var url = "https://myhost:5001";
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
var httpClient = new HttpClient(handler);
var channel = GrpcChannel.ForAddress(url, new GrpcChannelOptions { HttpClient = httpClient });
var client = new Greeter.GreeterClient(channel);
var answer = client.SayHello(new HelloRequest() { Name = "111" });
Console.WriteLine(answer.Message);
}
When I run the client application on my Windows 10 computer, I get an error:
Error starting gRPC call. HttpRequestException: Requesting HTTP version 2.0 with version policy RequestVersionOrHigher while server offers only version fallback.
On the server side error:
Microsoft.AspNetCore.Server.Kestrel[0]. HTTP/2 over TLS was not negotiated on an HTTP/2-only endpoint.
When I run the client application on Windows Server 2016 without IIS, I get an error:
Bad gRPC response. Response protocol downgraded to HTTP/1.1.
On the Windows Server 2016 with IIS and other Windows 10 computers client application works fine.
Do I have to install or enable any Windows components or install additional libraries or frameworks in order for ALPN to be enabled?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
As it turned out, our security guards installed a DLP system to control information leakage. Apparently, the system incorrectly intercepts and replaces packets…
Sorry, I don’t know.
I suggest opening an issue at https://github.com/dotnet/runtime and providing information there. The underlying TLS connection and protocol negotiation happens in .NET library code. gRPC just happens to be using it.