登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-27 18:03:34 258浏览 收藏

你在学习Golang相关的知识吗?本文《将 C# GRPC 客户端连接到 Go Server》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

我尝试从 c# 客户端连接到 go grpc 服务器,但客户端通道无法连接到服务器。

我希望连接到由 go 编写和托管的 grpc 服务器以及用 c# 编写的客户端时应该不会出现问题。

我的 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)
}

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

conn, err := grpc.dial("localhost:50051", grpc.withinsecure(), grpc.withblock())

但是当我尝试使用 grpc.net.client 包使用 c# 客户端进行连接时,如下所示:

var channel = grpcchannel.foraddress(@"http://localhost:50051", new grpcchanneloptions
{
   credentials = channelcredentials.insecure
});

我收到 grpc.core.rpcexception:'status(statuscode=internal, detail="bad grpc 响应。响应协议降级到 http/1.1。")' 异常。

或者通过使用 grpc.core 包,如下所示:

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

导致 grpc.core.rpcexception: 'status(statuscode=unavailable, detail="无法连接到所有地址")' 异常。

我已经尝试了在 c# 客户端中设置和不设置 http2unencryptedsupport 的两种方法,但对于无法连接的原因我已经没有任何想法了。

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


解决方案


对于 .net 3.x,设置:

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

并使用 http 作为 url 中的协议。

对于 .net 5.0,请使用 grpc.net.client 版本 2.32.0 或更高版本。

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

以上就是《将 C# GRPC 客户端连接到 Go Server》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>