Search code examples
grpc-dotnet

Why am I getting an Grpc.Core.RpcException when trying to get data from gRPC service written .net core


I have a gRPC service (written in .net core 3.1) deployed in windows server as a self hosted service running in kerstel.

enter image description here I added the below configuration to get the service running in https.

"Kestrel": {
"EndpointDefaults": {
  "Protocols": "Http2"
},
"Endpoints": {
  "HttpsInlineCertFile": {
    "Url": "https://hostname:8081",
    "Protocols": "Http2",
    "Certificate": {
      "Path": "path to .pfx file",
      "Password": "Super secret password"
    }
  }
}

Using the below code

using GrpcChannel channel = GrpcChannel.ForAddress(httpsHost);
var client = new MyClient(channel);
var response = client.GetEntity(RequestCreator.GetRequest());
Console.WriteLine("Recieved: " + response.ToString());

I get the an below exception and the inner exception is null.

Status(StatusCode=Internal, Detail="Error starting gRPC call: The SSL connection could not be established, see inner exception.")

enter image description here

Is there anything I should be adding to get the client to get the data from the service?

Thanks in advance


Solution

  • Sorry for the late response to my question. The problem was the proxy. What I ended up doing is create a webproxy, add the gRPC service URL to the bypassArrayList, create httpClientHandler, assigned the created proxy then create a httpClient that uses that httpClientHandler.

                var proxy = new WebProxy
                {
                    Address = new Uri(proxyServer),
                    UseDefaultCredentials = true,
                    BypassProxyOnLocal = true,
                    Credentials = CredentialCache.DefaultNetworkCredentials
                };
    
                proxy.BypassArrayList.Add(serviceUrl);
    
                var httpClientHandler = new HttpClientHandler
                {
                    Proxy = proxy,
                    DefaultProxyCredentials = CredentialCache.DefaultNetworkCredentials,
                    UseDefaultCredentials = true,
                    UseProxy = true,
                };
                var client = new HttpClient(httpClientHandler)
                {
                    DefaultRequestVersion = HttpVersion.Version20
                };
    
                using var channel = GrpcChannel.ForAddress(serviceUrl, new GrpcChannelOptions
                {
                    HttpClient = client
                });