You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过主机名连接gRPC服务器的技术问题求助

.NET 6 gRPC服务单独启动后无法通过主机名/IP连接

在VS中同时调试服务器和客户端时,gRPC连接完全正常;但单独启动服务器控制台程序后,同一设备通过主机名或IP无法建立连接。

环境与代码细节

我创建了一个基础的.NET 6「ASP.Net Core gRPC Service」Greeter应用,同时添加控制台客户端来隔离问题:

  • 服务器端未做任何修改,proto文件中设置GrpcServices="Both"
  • 客户端通过以下配置引用服务器的proto文件:
<ItemGroup>
    <Protobuf Include="..\GRPC-Server\Protos\greet.proto" GrpcServices="Both" />
</ItemGroup>

客户端核心实现代码:

var channel = GrpcChannel.ForAddress("https://localhost:5001"); 
var greeterClient = new Greeter.GreeterClient(channel);

HelloRequest request = new HelloRequest()
{
    Name = "Stranger"
};

HelloReply response = greeterClient.SayHello(request);

Console.WriteLine(response.Message);

Console.ReadLine();

不同连接方式的异常表现

  1. 使用主机名连接:将客户端地址改为https://MYHOSTNAME:5001,抛出异常:

"No connection could be made because the target machine actively refused it."

  1. 使用本地IP连接:将客户端地址改为https://127.0.0.1:5001,出现证书不匹配异常:

"The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch"

  1. 跳过证书验证后:127.0.0.1可以正常连接,但主机名仍提示连接被拒绝。

已尝试的操作

  • 为5000-5001端口创建了防火墙入站/出站规则
  • telnet测试:localhost和127.0.0.1可正常连接5001端口,但主机名无法连接
  • 在另一台设备上测试,结果完全相同

额外尝试与困惑

  • 在VS中修改launchSettings.json的applicationUrl为静态IP后,主机名可以正常连接;但改为主机名时,服务器会监听https://[::]:5001,导致连接失败
  • Release构建下不知道如何配置主机/端口,添加生产配置文件没有效果
  • 考虑过配置自定义证书,但缺乏相关经验,担心引入新问题

内容的提问来源于stack exchange,提问作者Serge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:38:23