Has anyone deployed .NET Core in a microservice with gRPC server in docker using ubuntu 18.04 Has anyone deployed .NET Core in a microservice with gRPC server in docker using ubuntu 18.04 docker docker

Has anyone deployed .NET Core in a microservice with gRPC server in docker using ubuntu 18.04


I found a solution which works for me, it took me just 3 weeks to figure it out.

There are 3 main concerns:

  1. Docker exposed ports
  2. Kestrel configuration
  3. Client channel configuration

Kestrel should be set with following parameters in appsetting.json or in the program cs which one you prefer:

  "Kestrel": {    "EndPoints": {      "Grpc": {        "Url": "http://*:8080",        "Protocols": "Http2"      },      "webApi": {        "Protocols": "Http1",        "Url": "http://*:80"      }    }  }

OR

ConfigureKestrel(options =>                {                    options.ListenAnyIP(80, listenOptions => listenOptions.Protocols = HttpProtocols.Http1); //webapi                    options.ListenAnyIP(8080, listenOptions => listenOptions.Protocols = HttpProtocols.Http2); //grpc                })

After this setting, you start the docker with following commands (ports and ip may differ as you wish, make sure there is no other thing listening on those ports ips):

docker run -p 127.0.0.40:8080:80 -p 127.0.0.40:8081:8080 --env ASPNETCORE_ENVIRONMENT=Development -it my_project -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_URLS=http://+

After this u have to configure your channel (do not use the ".AddGrpcClient" extension from Grpc.AspNetCore):

services.AddSingleton(x =>                        {                            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);                            return GrpcChannel.ForAddress("127.0.0.40:8081",                                //TODO:GRPC Root SSL Creds                                channelOptions: new GrpcChannelOptions()                                {                                    Credentials = ChannelCredentials.Insecure                                });                        });

After this you create your clients as you wish with the Channel you created and it should be fine whatever you want to do. No other method worked for me.

UPDATE:I found a way to make it work with "AddGrpcClient" from Grpc.AspNetCore.

services.AddGrpcClient<ControllerGuide.GuideClient>((x, opt) =>{    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);    opt.Address = new Uri("UriGRPC:5001");    opt.ChannelOptionsActions.Add(act =>    {        act.Credentials = ChannelCredentials.Insecure;    });});