简体   繁体   English

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

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

I'll be immensly grateful if you'd tell me what's causing the problem and how to fix it.如果您能告诉我问题的原因以及解决方法,我将不胜感激。

PS Sorry for posting all the code, it's just I'm not sure which exact part is relevant to the problem. PS 很抱歉发布所有代码,只是我不确定哪个确切部分与问题相关。

Here's the full text of exception:这是异常的全文:

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. the destination computer rejected the connection request. ", DebugException =" System.Net.Http.HttpRequestException: The connection was not established because the destination computer rejected the connection request. —-> System.Net.Sockets.SocketException (10061): Connection not established because the destination computer rejected the connection request. at System.Net.Http.ConnectHelper.ConnectAsync (String host, Int32 port, CancellationToken cancellationToken) —- End of inner exception stack trace —- at System.Net.Http.ConnectHelper.ConnectAsync (String host, Int32 port, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync (HttpRequestMes 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 request, CancellationToken cancellationToken) at Grpc.Net.Client.Internal.GrpcCall 2.RunCall (HttpRequestMessage request, Nullable 1 timeout) ") ' 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 超时)”)'

Here's the Server code:这是服务器代码:

Program.cs程序.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工人.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>();
            });
        }
    }
}

Here's the Client Code这是客户端代码

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

Explicit listening on port 5001 in the server resolved the issue.在服务器中的端口 5001 上进行显式侦听解决了该问题。

I was facing a similar error and maybe the solution that I found could be helpful for someone.我遇到了类似的错误,也许我找到的解决方案对某人有帮助。 Kestrel doesn't support HTTP/2 with TLS on macOS, so as Microsoft documentation recommends, Kestrel must configure an HTTP/2 endpoint without TLS in Program.cs: 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);
});

NOTE: in the preceding code, replace the localhost port number 5268 with the HTTP (not HTTPS) port number specified in Properties/launchSettings.json within the gRPC service project.注意:在前面的代码中,将localhost端口号 5268 替换为 gRPC 服务项目中Properties/launchSettings.json中指定的 HTTP(不是 HTTPS)端口号。

With that configuration, everything works fine when running locally, but when running in production or in docker, for example, I started facing the Grpc.Core.RpcException , to solve that problem, I changed the Kestrel configuration to ListenAnyIP , as this way, everything will work fine locally and also in a production environment:使用该配置,在本地运行时一切正常,但在生产环境或 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.

相关问题 Grpc.Core.RpcException:'状态(状态代码=“不可用”,详细信息=“空更新”, - Grpc.Core.RpcException: 'Status(StatusCode=“Unavailable”, Detail=“Empty update”, Grpc.Core.RpcException StatusCode Unavailable Channel 处于状态 TRANSIENT_FAILURE - Grpc.Core.RpcException StatusCode Unavailable Channel is in state TRANSIENT_FAILURE 为什么要通过Google Cloud Firestore 1.0.0-beta05 C#SetAsync获取Grpc.Core.RpcException StatusCode =不可用,Detail =“连接失败”? - Why getting Grpc.Core.RpcException StatusCode=Unavailable, Detail=“Connect Failed”, with Google Cloud Firestore 1.0.0-beta05 C# SetAsync? Google Vision API无法正常运行Grpc.Core.RpcException - Google Vision API not working Grpc.Core.RpcException 从 .NET MAUI 应用程序调用 gRPC 服务时出现 Grpc.Core.RpcException - Grpc.Core.RpcException when calling gRPC service from .NET MAUI app Grpc.Core.RpcException 方法未在 C# 客户端和 Java 服务器中实现 - Grpc.Core.RpcException method is unimplemented with C# client and Java Server Grpc.Core.RpcException“无法反序列化响应消息..”或“InvalidOperationException:长度不匹配” - Grpc.Core.RpcException "Failed to deserialize response message.." or "InvalidOperationException: Length mismatch" 从ASP.NET在本地访问Google Cloud Datastore会引发Grpc.Core.RpcException:“缺少权限或权限不足。” - Accessing Google Cloud Datastore locally from ASP.NET throws Grpc.Core.RpcException: “Missing or insufficient permissions.” C# 程序将数据保存到 Google FirestoreDB 在某些计算机上失败(状态代码=不可用;grpc_status:14) - C# Program saving data to Google FirestoreDB fails on some computers (StatusCode=Unavailable; grpc_status: 14) C# gRPC localhost - 启动 gRPC 调用时出错:不知道这样的主机 - C# gRPC localhost - Error starting gRPC call: No such host is known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM