Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="无法连接到所有地址;最后一个错误:UNKNOWN: ipv4:192.168.1.1:443: 无法建立连接,因为目标计算机主动拒绝了连接。")
Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="failed to connect to all addresses; last error: UNKNOWN: ipv4:192.168.1.1:443: No connection could be made because the target machine actively refused it.")
ID: dotnet/grpc-unavailable-connectivity
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| net6.0 | active | — | — | — |
| net7.0 | active | — | — | — |
| net8.0 | active | — | — | — |
| net9.0 | active | — | — | — |
根因分析
gRPC 客户端无法到达服务器,因为服务器未运行、端口被防火墙阻止或端点地址不正确。
English
The gRPC client cannot reach the server because the server is not running, the port is blocked by a firewall, or the endpoint address is incorrect.
官方文档
https://learn.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-8.0解决方案
-
Verify the server is running and listening on the expected port. Use 'netstat -an | findstr :443' (Windows) or 'ss -tuln | grep 443' (Linux) to check. Restart the gRPC server service if needed.
-
Check firewall rules. For Windows, run 'netsh advfirewall firewall add rule name="gRPC" dir=in action=allow protocol=TCP localport=443'. For Linux, use 'ufw allow 443/tcp' or adjust iptables.
-
Ensure the client endpoint URL matches the server's listening address. For example, if the server uses 'https://localhost:5001', the client should set 'new GrpcChannel.ForAddress("https://localhost:5001")'. Use DNS resolution with 'nslookup server.example.com' to verify IP.
无效尝试
常见但无效的做法:
-
Increasing the deadline or timeout in the gRPC call
95% 失败
The server is not responding at all; a longer timeout only delays the failure but does not establish a connection.
-
Disabling SSL/TLS on the client side
80% 失败
If the server requires HTTPS, disabling TLS will cause a different error (e.g., protocol mismatch), not fix the connection refusal.
-
Changing the port number arbitrarily
90% 失败
Unless the correct port is used, the connection will still be refused; this is a guess rather than a fix.