简体   繁体   English

将 C# GRPC 客户端连接到 Go 服务器

[英]Connect C# GRPC client to Go Server

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.我正在尝试从 C# 客户端连接到 Go GRPC 服务器,但客户端通道无法连接到服务器。

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#.我希望连接到由 Go 编写和托管的 GRPC 服务器和用 C# 编写的客户端应该没有问题。


My Go Server connection is setup thus:我的 Go 服务器连接是这样设置的:

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

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:但是,当我尝试使用 C# 客户端使用Grpc.Net.Client package 进行连接时,如下所示:

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.我得到一个Grpc.Core.RpcException: 'Status(StatusCode=Internal, Detail="Bad gRPC response. Response protocol downgraded to HTTP/1.1.")'异常。

Or by using the Grpc.Core package as thus:或者通过使用Grpc.Core package 如下:

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.导致Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'异常。

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.我已经尝试了在 C# 客户端中设置和不设置Http2UnencryptedSupport的两种方法,但我对无法连接的原因一无所知。

Any suggestions on where I am going wrong would be appreciated.任何关于我哪里出错的建议将不胜感激。

For .NET 3.x, set:对于 .NET 3.x,设置:

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

and use http for the protocol in the URL.并使用http作为 URL 中的协议。

For .NET 5.0, use version Grpc.Net.Client version 2.32.0 or later.对于 .NET 5.0,使用版本Grpc.Net.Client版本 2.32.0 或更高版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM