2

I have asp.net core 3.1 Grpc Server and asp.net core 3.1 Grpc client application. Intermittently I see the below error happening :

Status(StatusCode=Internal, Detail="Error starting gRPC call. HttpRequestException: Connection reset by peer SocketException: Connection reset by peer")

Here goes the Grpc client code:

public static async Task < TResponse > CallService < TResponse > (string urlGrpc, Func < GrpcChannel, Task < TResponse >> func) {
    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);

    var channel = GrpcChannel.ForAddress(urlGrpc);
    // var channel = GrpcChannel.ForAddress("http://localhost:32769");            
    /*
 using var httpClientHandler = new HttpClientHandler
 {
     ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
 };

 */
    Log.Information("Creating grpc client base address urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress} ", urlGrpc, channel.Target);

    try {
        return await func(channel);
    } catch(RpcException e) {
        Log.Error("Error calling via grpc: {Status} - {Message}", e.Status, e.Message);
        return
    default;
    } finally {
        AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
        AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", false);
    }
}

I have been using Azure Dev Spaces for debugging in my local environment.

Can anyone help me here by providing their guidance to fix this issue.

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • 1
    Are you sure this isn't caused by transient connectivity issues, like the ones you'd encounter in production? You can't fix a bad connection in code, you need to handle the problem eg by retrying or switching to another service. You can use libraries like Polly to retries easier – Panagiotis Kanavos Jul 03 '20 at 09:45
  • The error you're getting probably means you're simply being rejected by the server. This could be for numerous reasons - e.g. server only accepts secure connections but you are trying to connect without TLS (http://localhost/) - in which case the ServerCertificateCustomValidationCallback doesn't make any sense. Also, setting the appcontext.SetSwitch back and forth seems suspicious. – Jan Tattermusch Jul 14 '20 at 08:44

0 Answers0