13

I have a ASP.NET Core application that has two endpoints. One is the MVC and the other is the Grpc. I need that the kestrel publishs each endpoint on different sockets. Example: localhost:8888 (MVC) and localhost:8889 (Grpc).

I know how to publish two endpoints on Kestrel. But the problem is that it's publishing the MVC and the gRPC on both endpoints and I don't want that. This is because I need the Grpc requests use Http2. On the other hand, I need that MVC requests use Http1

on my Statup.cs I have

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

I need a way to make endpoints.MapGrpcService<ComunicacaoService>(); be published on one socket and the endpoints.MapControllerRoute("default","{controller}/{action=Index}/{id?}"); on another one.

hackr
  • 79
  • 7
Douglas Ramos
  • 531
  • 1
  • 5
  • 10

1 Answers1

18

Here is the configuration that works for me:

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>();
       });

Alternatively in appsettings.json:

  "Kestrel": {
    "Endpoints": {
      "Grpc": {
        "Protocols" :  "Http2",
        "Url": "http://localhost:55001"
      },
      "webApi": {
        "Protocols":  "Http1",
        "Url": "http://localhost:55002"
      }
    }
  }
ghord
  • 13,260
  • 6
  • 44
  • 69
  • 1
    Thanks for the answer. I have yet a little question. I'm not sure how to handle this on the startup file. If I think right, your code will publish both services on both sockets, cause that is nothing explict saying to publish one service on one port and the other on another one – Douglas Ramos Sep 18 '19 at 20:16
  • 2
    From what I understand both middlewares will be present on both endpoints, just that GRPC will work only with HTTP2 – ghord Sep 19 '19 at 11:01
  • I see. This is not exactly what I'm looking for but a apreciate the time to write an answer for me. Thanks man! – Douglas Ramos Sep 26 '19 at 11:10
  • 2
    Have you tried instantiating, building and running two IHostBuilders (each configured individually, one for gRPC, one for MVC) and then Task.WhenAll for both? – Stonehead Oct 17 '19 at 09:29
  • @Stonehead Actually, no. This seems a really great idea. I'll try it out, thanks – Douglas Ramos Jan 01 '20 at 18:37