簡體   English   中英

Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call

[英]Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call

如果您能告訴我問題的原因以及解決方法,我將不勝感激。

PS 很抱歉發布所有代碼,只是我不確定哪個確切部分與問題相關。

這是異常的全文:

Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call. HttpRequestException: The connection was not established because the destination computer rejected the connection request. SocketException: The connection was not established. 目標計算機拒絕了連接請求。", DebugException =" System.Net.Http.HttpRequestException: 連接未建立,因為目標計算機拒絕了連接請求。 —-> System.Net.Sockets.SocketException (10061): 連接未建立,因為目標計算機拒絕了連接請求。在 System.Net.Http.ConnectHelper.ConnectAsync(String 主機,Int32 端口,CancellationToken cancellationToken)——內部異常堆棧跟蹤結束——在 System.Net.Http.ConnectHelper.ConnectAsync(String主機,Int32 端口,CancellationToken cancellationToken)在 System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMes sage request, Boolean allowHttp2, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync (HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync (HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net .Http.RedirectHandler.SendAsync(HttpRequestMessage 請求,CancellationToken cancellationToken)在 Grpc.Net.Client.Internal.GrpcCall 2.RunCall (HttpRequestMessage request, Nullable 1 超時)”)'

這是服務器代碼:

程序.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>();
                });
    }
}

工人.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>();
            });
        }
    }
}

這是客戶端代碼

程序.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();
        }
    }
}

在服務器中的端口 5001 上進行顯式偵聽解決了該問題。

我遇到了類似的錯誤,也許我找到的解決方案對某人有幫助。 Kestrel 在 macOS 上不支持帶 TLS 的 HTTP/2,因此正如Microsoft 文檔所建議的那樣,Kestrel 必須在 Program.cs 中配置不帶 TLS 的 HTTP/2 端點:

builder.WebHost.ConfigureKestrel(options =>
{
    // Setup a HTTP/2 endpoint without TLS.
    options.ListenLocalhost(5268, o => o.Protocols =
        HttpProtocols.Http2);
});

注意:在前面的代碼中,將localhost端口號 5268 替換為 gRPC 服務項目中Properties/launchSettings.json中指定的 HTTP(不是 HTTPS)端口號。

使用該配置,在本地運行時一切正常,但在生產環境或 docker 中運行時,例如,我開始面臨Grpc.Core.RpcException ,為了解決該問題,我將 Kestrel 配置更改為ListenAnyIP ,這樣,一切都會在本地和生產環境中正常工作:

builder.WebHost.ConfigureKestrel(options =>
{
    // Setup a HTTP/2 endpoint without TLS.
    options.ListenAnyIP(5268, o => o.Protocols = HttpProtocols.Http2);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM