0

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?

Clément Jean
  • 1,735
  • 1
  • 14
  • 32
CMLEE
  • 23
  • 4
  • double check if you are using right `url` and `port` number is valid one, also have a look at below post might be something related to what you are facing. https://stackoverflow.com/questions/61661696/httpclient-throwing-an-error-occurred-while-sending-the-request – mabiyan Mar 08 '22 at 06:01
  • Im checking url but it doesnt solve my problem... But exception codes are changing! it makes my mind, Maybe I can solve this problem. Thx – CMLEE Mar 08 '22 at 07:10

2 Answers2

0

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.

Clemens
  • 588
  • 6
  • 9
0

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.

Clément Jean
  • 1,735
  • 1
  • 14
  • 32