将 gRPC 服务绑定到 aspnetcore 中的特定端口

Lau*_*nce 20 .net c# .net-core grpc asp.net-core

使用aspnetcore 3.1nugetGrpc.AspNetCore包,我成功地使 gRPC 服务与标准 asp.net 控制器一起成功运行,如本教程中所述。

不过,我想将 gRPC 服务绑定到特定端口(例如 5001),如果可能的话最好通过配置而不是代码。这是因为我想限制 gRPC 服务的公开方式。

我最接近的是RequireHost在映射端点时使用的:

// Startup.cs
public void Configure(IApplicationBuilder app)
{
    // ...
    
    app.useEndpoints(endpoints => 
    {
        endpoints.MapGrpcService<MyService>()
            .RequireHost("0.0.0.0:5001");
    }); 
}
Run Code Online (Sandbox Code Playgroud)

这似乎符合我的要求,但我找不到任何有关它的文档,并且它需要在每个服务的代码中进行配置。也许有更好的方法?

L_J*_*L_J 12

ASP.NET Core 6.0可以在文件中更改端口Properties > launchSettings.jsonVisual Studio但仅当您从或运行服务器时才会考虑此文件VS Code

我试图使用该.exe文件直接运行服务器进行测试。服务器使用默认端口运行:"http://localhost:5000;https://localhost:5001"

最后,我将其从appsettings.json文件中更改为.exe

  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "https://localhost:7005",
        "Protocols": "Http1AndHttp2"
      },
      "gRPC": {
        "Url": "http://localhost:5005",
        "Protocols": "Http2"
      }
    }
Run Code Online (Sandbox Code Playgroud)


Mar*_*son 11

这适用于 Kestrel(服务器端):

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureKestrel(options =>
    {
       options.Listen(IPAddress.Loopback, 5000);
       options.Listen(IPAddress.Loopback, 5005, configure => configure.UseHttps());
    });
    webBuilder.UseStartup<Startup>();
});
Run Code Online (Sandbox Code Playgroud)

客户端:

 var httpHandler = new HttpClientHandler
 {
     ServerCertificateCustomValidationCallback =
     HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
 };  

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
                
using var channel = GrpcChannel.ForAddress("https://localhost:5005", new GrpcChannelOptions { HttpHandler = httpHandler } );
            
var client = new Greeter.GreeterClient(channel);
Run Code Online (Sandbox Code Playgroud)

笔记:

 var httpHandler = new HttpClientHandler
 {
     ServerCertificateCustomValidationCallback =
     HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
 };  
Run Code Online (Sandbox Code Playgroud)

当您拥有没有信任链的自签名证书时(主要是在开发时)。

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
Run Code Online (Sandbox Code Playgroud)

是为了支持http。


Mar*_*cky 8

需要配置中间件

app.UseRouting();
app.MapWhen(context => {
   return context.Connection.LocalPort == 1000
}, newApp => {
   newApp.UseRouting();
   newApp.UseEndpoints(endpoints =>
   {
        endpoints.MapGrpcService<Service1>();
   }
});
app.MapWhen(context => {
   return context.Connection.LocalPort == 2000
}, newApp => {
   newApp.UseRouting();
   newApp.UseEndpoints(endpoints =>
   {
        endpoints.MapGrpcService<Service2>();
   }
});
Run Code Online (Sandbox Code Playgroud)


Bra*_*ang 5

据我所知,没有其他方法可以为GRPC服务设置特定端口。

grpc服务也运行在asp.net core kestrel服务器上,服务器将监听端口而不是服务。

如果你的asp.net core应用程序只有GRPC服务,你可以将kestrel服务器的监听端口设置为5001。

如果您有多个服务,例如 MVC Web api 或其他服务,则 RequireHost 是仅允许特定端口访问 grpc 服务的最佳解决方法。

如果你想提示GRPC服务的路由系统需要指定的端口,你可以使用以下端口:

 routes.MapGrpcService<MyService>().RequireHost("*:5001");
Run Code Online (Sandbox Code Playgroud)