Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call
Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "Error starting gRPC call
如果您能告诉我问题的原因以及解决方法,我将不胜感激。
P.S。很抱歉发布所有代码,只是我不确定哪个部分与问题相关。
异常全文如下:
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, Nullable
1 timeout) ") '
这是服务器代码:
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>();
});
}
}
}
这是客户端代码
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();
}
}
}
在服务器中显式侦听端口 5001 解决了该问题。
如果您能告诉我问题的原因以及解决方法,我将不胜感激。
P.S。很抱歉发布所有代码,只是我不确定哪个部分与问题相关。
异常全文如下:
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.GrpcCall
2.RunCall (HttpRequestMessage request, Nullable
1 timeout) ") '
这是服务器代码:
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>();
});
}
}
}
这是客户端代码
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();
}
}
}
在服务器中显式侦听端口 5001 解决了该问题。