question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to enable ALPN in .Net 5 projects

See original GitHub issue

In 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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SLavaLLcommented, Aug 4, 2021

As it turned out, our security guards installed a DLP system to control information leakage. Apparently, the system incorrectly intercepts and replaces packets…

0reactions
JamesNKcommented, Jul 15, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure endpoints for the ASP.NET Core Kestrel web ...
Learn about configuring endpoints with Kestrel, the cross-platform web server for ASP.NET Core.
Read more >
How to implement TLS-ALPN in .NET C# for a HTTP/2 server
Unfortunately, .NET's SslStream has no ability to specify application protocols as part of the TLS authentication, so it can't support ALPN.
Read more >
Troubleshoot gRPC on .NET
Create a new .NET Core class library project. In the new project, add references to enable C# code generation from .proto files:.
Read more >
Enable ALPN parameters to be supplied during the TLS ...
ALPN (Application Layer Protocol Negotiation) [1] is a TLS extension to enable clients and servers to negotiate the application-level protocol that will
Read more >
How to use HTTP/2 with HttpClient in .NET 6.0
In this article, we will look into the different ways to configure HttpClient to use HTTP/2 standard in .NET 6.0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found