将 C# GRPC 客户端连接到 Go Server

Ste*_*oss 7 c# go grpc grpc-go

我正在尝试从 C# 客户端连接到 Go GRPC 服务器,但客户端通道无法连接到服务器。

我希望连接到由 Go 编写和托管的 GRPC 服务器以及用 C# 编写的客户端时应该没有问题。


我的 Go Server 连接是这样设置的:

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)
}
Run Code Online (Sandbox Code Playgroud)

这是相当标准的,我知道我的服务器正在工作,因为我能够使用提供的端口与 Go 客户端连接:

conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用 C# 客户端使用Grpc.Net.Client包进行连接时:

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

我得到一个Grpc.Core.RpcException: 'Status(StatusCode=Internal, Detail="Bad gRPC response. Response protocol downgraded to HTTP/1.1.")'例外。

或者通过使用这样的Grpc.Core包:

var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
Run Code Online (Sandbox Code Playgroud)

导致Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'异常。

我已经Http2UnencryptedSupport在 C# 客户端中尝试了设置和未设置的两种方法,但我对为什么无法连接的想法一无所知。

任何关于我哪里出错的建议将不胜感激。

Tyl*_*opp 2

对于 .NET 3.x,设置:

AppContext.SetSwitch(
    "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
Run Code Online (Sandbox Code Playgroud)

并用于httpURL 中的协议。

对于 .NET 5.0,请使用版本Grpc.Net.Client2.32.0 或更高版本。

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