Home > database >  grpc RpcException Occured when I send
grpc RpcException Occured when I send

Time:03-08

I just want Connect and send string to Grpc server and get string.

The server is written by python, And I make client by C#

It works in python client

This is my client Code and Exception log.

Client Code

        static async Task Main(string[] args)
        {
            using var channel = GrpcChannel.ForAddress("http://1.2.3.4:5555");
            var client = new GRPCSerivce.GRPCSerivceClient(channel);
            var reply = await client.SynAck_ResultStringAsync(new DataRequest { Keys = "12345" });
            Console.WriteLine($"Return: {reply.Datas}");
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }

The error occured in this line

var reply = await client.SynAck_ResultStringAsync(new DataRequest { Keys = "12345" });

and this is exception code

Grpc.Core.RpcException: 'Status(StatusCode="Unavailable", 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.
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Grpc.Net.Client.Internal.GrpcCall`2.RunCall(HttpRequestMessage request, Nullable`1 timeout)")'

The server has already explicitly declared the port number.

What makes this exception?

CodePudding user response:

As far as I know you need to set

AppContext.SetSwitch(
    "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

if you want to use insecure connections. Try using https or setting the switch. Please have a look here.

CodePudding user response:

If you do not need to use a TLS connection, try constructing the channel by doing something like:

using var channel = new Channel("1.2.3.4", 5555, ChannelCredentials.Insecure);

If that still doesn't work, you might give us more information on your server and your client (implementation, environment, ...). After that I can update my answer.

  • Related