1

我有 asp.net core 3.1 Grpc 服务器和 asp.net core 3.1 Grpc 客户端应用程序。我间歇性地看到以下错误发生:

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

下面是 Grpc 客户端代码:

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);
    }
}

我一直在使用 Azure Dev Spaces 在本地环境中进行调试。

任何人都可以通过提供解决此问题的指导来帮助我。

4

0 回答 0