doliferous
doliferous

Reputation: 31

Code-first Grpc.Net can't connect to remote machine

I'm trying to update my code from Grpc.Core to Grpc.Net and I'm starting with the client/server connection portion.

On the server side, I'm configuring Kestrel like so:

private void CreateServer()
        {
            _server = Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseKestrel();

                    webBuilder.ConfigureKestrel(options =>
                    {
                        options.Listen(IPAddress.Any, _port, listenOptions =>
                        {
                            listenOptions.Protocols = HttpProtocols.Http2;
                            listenOptions.UseHttps();
                        });
                    });

                    webBuilder.ConfigureServices(services =>
                    {
                        services.AddCodeFirstGrpc(options =>
                        {
                            options.MaxReceiveMessageSize = int.MaxValue;
                            options.MaxSendMessageSize = int.MaxValue;
                        });
                    });

                    webBuilder.Configure((IApplicationBuilder app) =>
                    {
                        app.UseRouting();
                        app.UseEndpoints(endpoints => endpoints.MapGrpcService<GrpcService>());
                    });

                }).Build();

            _server.RunAsync();
        }

And on the client side I'm trying to connect like so:

        private IGrpcService CreateClient()
        {
            var channelOption = new GrpcChannelOptions
            {
                MaxReceiveMessageSize = int.MaxValue,
                MaxSendMessageSize = int.MaxValue,
            };
            
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            var address = $"https://{_host}:{_port}";
            Channel = GrpcChannel.ForAddress(address, channelOption);
            IGrpcService client = Channel.CreateGrpcService<IGrpcService>();

            client.SayHello("TESTING123");

            return client;
        }

And I would get the following error:

Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: No connection could be made because the target machine actively refused it. (testmachine1:59) SocketException: No connection could be made because the target machine actively refused it.", DebugException="System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (testmachine1:59)

A few things to note are that the ports are definitely the same. I logged them to confirm and that I'm using hostname in the address. However, I've tried getting the IP Address by using Dns.GetHostAddresses(_host) and the same error happens. Also note that the client and server are 2 different machines, I checked the logs of the host machine and it did start and I see it listening to the port specified. This set up also used to work fine with Grpc.Core code-first configuration

Any ideas why this might be happening? Any help would be appreciated since I'm not sure what I'm doing wrong.

Upvotes: 0

Views: 1045

Answers (1)

doliferous
doliferous

Reputation: 31

Never mind I added app.UseDeveloperExceptionPage() in Configure and it gave me more logs to work with. It turns out I needed to add services.AddScoped(_ => new GrpcService()); in ConfigureServices to make it work. Although right now it only works for http so I needed to remove the UseHttps() line

Upvotes: 0

Related Questions