我正在嘗試從 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)}這是相當標準的,我知道我的服務器正在工作,因為我能夠使用提供的端口與 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.")'例外。或者通過Grpc.Core這樣使用包:var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);導致Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'異常。我已經在 C# 客戶端中嘗試了設置和不Http2UnencryptedSupport設置的兩種方法,但我對為什么無法連接沒有任何想法。任何關于我哪里出錯的建議將不勝感激。
1 回答

幕布斯6054654
TA貢獻1876條經驗 獲得超7個贊
對于 .NET 3.x,設置:
AppContext.SetSwitch( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
并http
用于 URL 中的協議。
對于 .NET 5.0,使用Grpc.Net.Client
版本 2.32.0 或更高版本。
- 1 回答
- 0 關注
- 345 瀏覽
添加回答
舉報
0/150
提交
取消