繁体   English   中英

在 Kestrel 上为 ASP.NET Core 上的两个不同端点发布两个不同的端点

[英]Publish two different endpoints on Kestrel for two different endpoints on ASP.NET Core

我有一个有两个端点的 ASP.NET Core 应用程序。 一个是MVC,另一个是Grpc。 我需要 kestrel 在不同的套接字上发布每个端点。 示例:localhost:8888 (MVC) 和 localhost:8889 (Grpc)。

我知道如何在 Kestrel 上发布两个端点。 但问题是它在两个端点上都发布了 MVC 和 gRPC,我不想要那样。 这是因为我需要 Grpc 请求使用 Http2。 另一方面,我需要 MVC 请求使用 Http1

在我的 Statup.cs 上我有

public void Configure(IApplicationBuilder app)
{
    // ....
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGrpcService<ComunicacaoService>();
        endpoints.MapControllerRoute("default",
                                      "{controller}/{action=Index}/{id?}");
    });
    // ...

我需要一种制作endpoints.MapGrpcService<ComunicacaoService>();的方法endpoints.MapGrpcService<ComunicacaoService>(); 发布在一个套接字和endpoints.MapControllerRoute("default","{controller}/{action=Index}/{id?}"); 在另一个。

这是对我有用的配置:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
           webBuilder.ConfigureKestrel(options =>
           {
               options.Listen(IPAddress.Loopback, 55001, cfg =>
               {
                   cfg.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2;
               });

               options.Listen(IPAddress.Loopback, 55002, cfg =>
               {
                   cfg.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1;
               }); 
           });

           webBuilder.UseStartup<Startup>();
       });

或者在 appsettings.json 中:

  "Kestrel": {
    "Endpoints": {
      "Grpc": {
        "Protocols" :  "Http2",
        "Url": "http://localhost:55001"
      },
      "webApi": {
        "Protocols":  "Http1",
        "Url": "http://localhost:55002"
      }
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM