How to enforce HTTP (instead of HTTPS) while calling gRPC service from Server-Side Blazor

944 views Asked by At

Okay, so long story short :

  • I have Blazor Server-Side app.
  • I have gRPC service
  • Both are running on localhost

Issue :

  • I can't make it connect via HTTP (though HTTPS works, with self-signed cert)

What I have tried :

  • Adding AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); to Blazor app Program.cs (at very start, before calling builder) and using services.AddGrpcClient<>(); to add my service client to DI
  • Creating client library (wrapper) for my gRPC service and adding same in constructor, just before creating client :
        public Client(string url)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            _channel = GrpcChannel.ForAddress(url);
            _client = new SvcTrlClient(_channel);
        }

And registering it to Blazor app's DI myself.

What I get when calling from Blazor :

Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The response ended prematurely.", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: The response ended prematurely.

Worth mentioning :

  • AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); works from console app

Is this enough to disable HTTPS on gRPC side?

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)

        .UseSerilog()
        .UseWindowsService()

        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseKestrel(options =>
            {
                options.Listen(IPAddress.Loopback, 5666, x => x.Protocols = HttpProtocols.Http2);
            });

            webBuilder.UseStartup<Startup>();
        });
    }
}
0

There are 0 answers