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

4

6

I'll be immensly grateful if you'd tell me what's causing the problem and how to fix it.

P.S. Sorry for posting all the code, it's just I'm not sure which exact part is relevant to the problem.

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 (HttpRequestMessage 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.GrpcCall2.RunCall (HttpRequestMessage request, Nullable1 timeout) ") '

Here's the Server code:

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

Here's the Client Code

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();
        }
    }
}
Breeding answered 29/11, 2021 at 9:6 Comment(1)
I suspect that options.ListenAnyIP(0, ...) will just bind on any port. You're specifying port 5001 in your client, so I suggest you explicitly listen on that port in the server.Di
B
5

Explicit listening on port 5001 in the server resolved the issue.

Breeding answered 29/11, 2021 at 11:9 Comment(0)
H
4

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:

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.

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:

builder.WebHost.ConfigureKestrel(options =>
{
    // Setup a HTTP/2 endpoint without TLS.
    options.ListenAnyIP(5268, o => o.Protocols = HttpProtocols.Http2);
});
Hilmahilt answered 26/1, 2023 at 13:35 Comment(0)
L
1

I've changed 'localhost' to '+' and it works for me:

"Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://+:80",
        "Protocols": "Http1AndHttp2"
      },
      "gRPC": {
        "Url": "http://+:9090",
        "Protocols": "Http2"
      }
    }
  }
Launder answered 11/11, 2023 at 18:26 Comment(0)
F
0

There is 'launchSettings.json' file in Properties folder of my gRPCServer project. After I updated the value of applicationUrl like following, it worked.

{
  "profiles": {
    "gRPCService": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": false,
      "applicationUrl": "http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Forestaysail answered 26/12, 2023 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.