0

如果你能告诉我是什么导致了问题以及如何解决它,我将不胜感激。

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

以下是异常的全文:

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 (String host, Int32 port, CancellationToken cancellationToken) ---- End of internal exception stack trace ---- at System.Net.Http.ConnectHelper。System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage 请求,CancellationToken cancelToken)处 System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage 请求,布尔 allowHttp2,CancellationToken cancelToken)处的 ConnectAsync(字符串主机,Int32 端口,CancellationToken cancelToken) .Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage 请求,布尔型 doRequestAuth,CancellationToken cancelToken)在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken)在 Grpc.Net.Client.Internal.GrpcCallHttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage 请求,CancellationToken cancelToken)在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage 请求,布尔 doRequestAuth,CancellationToken cancelToken)在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken)在 Grpc。 Net.Client.Internal.GrpcCallHttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage 请求,CancellationToken cancelToken)在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage 请求,布尔 doRequestAuth,CancellationToken cancelToken)在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken)在 Grpc。 Net.Client.Internal.GrpcCall2.RunCall (HttpRequestMessage request, Nullable1 超时) ") '

这是服务器代码:

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

1 回答 1

0

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

于 2021-11-29T11:09:08.827 回答