gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:38:53 25 4
gpt4 key购买 nike

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

我希望连接到由 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)
}

这是相当标准的,我知道我的服务器正在工作,因为我能够使用提供的端口与 Go 客户端连接:
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())

但是当我尝试使用 C# 客户端使用 Grpc.Net.Client 进行连接时包装如下:
var channel = GrpcChannel.ForAddress(@"http://localhost:50051", new GrpcChannelOptions
{
Credentials = ChannelCredentials.Insecure
});

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

或使用 Grpc.Core包装如下:
var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);

结果为 Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'异常(exception)。

我已经尝试了有和没有 Http2UnencryptedSupport 的两种方法在 C# 客户端中设置,但我已经没有关于为什么无法连接的想法。

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

最佳答案

对于 .NET 3.x,设置:

AppContext.SetSwitch(
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
并使用 http用于 URL 中的协议(protocol)。
对于 .NET 5.0,使用版本 Grpc.Net.Client版本 2.32.0 或更高版本。
来源: https://docs.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-5.0#call-insecure-grpc-services-with-net-core-client

关于c# - 将 C# GRPC 客户端连接到 Go Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62197286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com