Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "错误启动 gRPC 调用

Posted

技术标签:

【中文标题】Grpc.Core.RpcException: \'Status (StatusCode = "Unavailable", Detail = "错误启动 gRPC 调用【英文标题】:Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC callGrpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "错误启动 gRPC 调用 【发布时间】:2022-01-06 03:36:24 【问题描述】:

如果您能告诉我问题的原因以及解决方法,我将不胜感激。

附:抱歉发布所有代码,只是我不确定哪个确切部分与问题相关。

这里是异常的全文:

Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "错误启动 gRPC 调用。HttpRequestException:连接未建立,因为目标计算机拒绝了连接请求。SocketException:连接未建立。目标计算机拒绝连接请求。", DebugException =" System.Net.Http.HttpRequestException: 连接未建立,因为目标计算机拒绝了连接请求。 ----> System.Net.Sockets.SocketException (10061):连接未建立,因为目标计算机拒绝了连接请求。 在 System.Net.Http.ConnectHelper.ConnectAsync (字符串主机,Int32 端口,CancellationToken 取消令牌) ----内部异常堆栈跟踪结束---- 在 System.Net.Http.ConnectHelper.ConnectAsync (字符串主机,Int32 端口,CancellationToken 取消令牌) 在 System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage 请求,布尔 allowHttp2,CancellationToken cancelToken) 在 System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage 请求,CancellationToken cancelToken) 在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync (HttpRequestMessage 请求,布尔 doRequestAuth,CancellationToken 取消令牌) 在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken) 在 Grpc.Net.Client.Internal.GrpcCall2.RunCall (HttpRequestMessage request, Nullable1 超时)") '

这是服务器代码:

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace GrpcHostServer

    public class Program
    
        public static void Main(string[] args)
        
            CreateHostBuilder(args).Build().Run();
        

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices(services =>
                
                    services.AddHostedService<Worker>();
                );
    

Worker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GrpcHostServer.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace GrpcHostServer

    public class Worker : BackgroundService
    
        private readonly ILogger<Worker> _logger;

        public Worker(ILogger<Worker> logger)
        
            _logger = logger;
        

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        
            await Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(builder =>
                
                    builder
                        .ConfigureKestrel(options =>
                        
                            options.ListenAnyIP(0, listenOptions =>
                            
                                listenOptions.Protocols = HttpProtocols.Http2;
                            );
                        )
                        .UseKestrel()
                        .UseStartup<GrpcServerStartup>();
                )
                .Build()
                .StartAsync(stoppingToken);
        
    

    public class GrpcServerStartup
    
        public void ConfigureServices(IServiceCollection services)
        
            services.AddGrpc();

            services.AddSingleton<GreeterService>();
        

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            
                endpoints.MapGrpcService<GreeterService>();
            );
        
    

这是客户端代码

Program.cs

using Grpc.Net.Client;
using GrpcHostServer;
using System;
using System.Threading.Tasks;

namespace GrpcClient

    class Program
    
        static async Task Main(string[] args)
        
            var input = new HelloRequest  Name = "Boris" ;
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new Greeter.GreeterClient(channel);

            var reply = await client.SayHelloAsync(input);

            Console.WriteLine(reply.Message);

            Console.ReadLine();
        
    

【问题讨论】:

怀疑 options.ListenAnyIP(0, ...) 只会绑定到任何端口。您在客户端中指定端口 5001,因此我建议您在服务器中明确侦听该端口。 非常感谢,帮了大忙! 【参考方案1】:

在服务器的 5001 端口上进行显式侦听解决了该问题。

【讨论】:

以上是关于Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "错误启动 gRPC 调用的主要内容,如果未能解决你的问题,请参考以下文章

增加 Google PubSub 的截止时间超时