Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect C# GRPC client to Go Server

Tags:

c#

go

grpc

grpc-go

I'm attempting to connect to a Go GRPC Server from a C# Client but the client channel is failing to connect to the server.

I would expect that there should be no issues in connecting to a GRPC server that is written and hosted by Go and a client written in C#.


My Go Server connection is setup thus:

lis, err := net.Listen("tcp", ":50051")
if err != nil {
    log.Fatalf("Failed to listen: %v", err)
}

s := grpc.NewServer()

pb.Register**ServiceServer(s, &server{})
reflection.Register(s)
if err := s.Serve(lis); err != nil {
    log.Fatalf("Failed to serve: %v", err)
}

This is fairly standard and I know that my Server is working as I am able to connect with a Go client using the supplied port:

conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())

But when I attempt to connect using a C# Client using the Grpc.Net.Client package as thus:

var channel = GrpcChannel.ForAddress(@"http://localhost:50051", new GrpcChannelOptions
{
   Credentials = ChannelCredentials.Insecure
});

I get a Grpc.Core.RpcException: 'Status(StatusCode=Internal, Detail="Bad gRPC response. Response protocol downgraded to HTTP/1.1.")' exception.

Or by using the Grpc.Core package as thus:

var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);

results in a Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")' exception.

I've tried both methods with and without Http2UnencryptedSupport set within the C# client but I've run out of ideas on why I am unable to connect.

Any suggestions on where I am going wrong would be appreciated.

like image 965
Stephen Ross Avatar asked Jun 04 '20 14:06

Stephen Ross


People also ask

What is connect in C?

The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. The addrlen argument specifies the size of addr. The format of the address in addr is determined by the address space of the socket sockfd; see socket(2) for further details.

What is connect () function?

The connect function is used to create a connection to the specified destination. If socket s, is unbound, unique values are assigned to the local association by the system, and the socket is marked as bound.

Is connect blocking C?

connect is a blocking call by default, but you can make it non blocking by passing to socket the SOCK_NONBLOCK flag.

What does connect function do in socket programming?

The connect() call on a stream socket is used by the client application to establish a connection to a server. The server must have a passive open pending. A server that is using sockets must successfully call bind() and listen() before a connection can be accepted by the server with accept().


1 Answers

For .NET 3.x, set:

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

and use http for the protocol in the URL.

For .NET 5.0, use version Grpc.Net.Client version 2.32.0 or later.

Source: https://docs.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-5.0#call-insecure-grpc-services-with-net-core-client

like image 68
Tyler Kropp Avatar answered Oct 06 '22 08:10

Tyler Kropp