3

Earlier we were using .net core 2.1 API, In the same Project we were exposing two ports, One for rest API and another one for GRPC

Below is how our startup and Program file looks like

Startup file

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //Proces for GRPC server binding
        Server server = new Server
        {
            Services = { HealthCheck.BindService(new HealthCheckController()),
                         },
            Ports = { new ServerPort("0.0.0.0", 5001, ServerCredentials.Insecure) }
        };
        //Start GRPC server
        server.Start();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }

Program file

public static class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
    }
}

Now we want to migrate the same project to .net core 3.1, and want to maintain two seprate ports, one for GRPC and other one for rest API

Below is how our startup and Program file looks like

Startup file

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGrpcService<HealthCheckController>();
            endpoints.MapControllers();
        });
        //Start GRPC server

    }

Program file

    public static class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }


    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

But the problem is after migrating to .net core 3.1 we are only able to use both the services on the same port, any help on how we can separate ports for GRPC and Rest API is appreciated.

abhi
  • 1,059
  • 9
  • 19

1 Answers1

0

I'm not sure if or why .net 3.1 would require sharing the same port (Can I combine a gRPC and webapi app into a .NET Core 3.0 in C#? seems to suggest that, at least for 3.0, this was not the case). If you do need to share the port, you might want to look into using Kestrel with a reverse proxy (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#when-to-use-kestrel-with-a-reverse-proxy) which should be able to route the traffic according to its type.

Eric G
  • 4,018
  • 4
  • 20
  • 23