'Can I combine a gRPC and webapi app into a .NET Core 3.0 in C#?

I am using dot net core 3.0.

I have gRPC app. I am able to communicate to it through gRPC protocol.

I thought my next step would be add some restful API support. I modified my startup class to add controllers, routing etc..... When I try navigating to the API using a browser, I get an error "ERR_INVALID_HTTP_RESPONSE" no matter which protocol (http/https) and port I use. gRPC should be using 5001 and webapi using 8001.

heres my startup class:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc();
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();

        app.UseRouting();
        app.UseHttpsRedirection();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGrpcService<BootNodeService>();
            endpoints.MapControllers();

        });
    }
}

And my controller:

[ApiController]
[Route("[controller]")] 
public class AdminController : ControllerBase 
{ 
    [HttpGet] public string Get() 
    { return "hello"; } 
}

Any thoughts?

Thnx

EDIT: the entire project can be found at this repo.

EDIT: view of screen enter image description here



Solution 1:[1]

I found the solution. I didn't mention I was running on MacOS and using Kestrel (and it appears the combination of MacOS AND Kestrel is the problem). I apologize for that missing information.

The solution is similar to what is here. I had to add a call to options.ListenLocalhost for the webapi port.

here's the code:

public class Program
{
    public static void Main(string[] args)
    {
       IHostBuilder hostBuilder = CreateHostBuilder(args);
       IHost host = hostBuilder.Build();
       host.Run();
    }

    // Additional configuration is required to successfully run gRPC on macOS.
    // For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(options =>
                {
                    options.ListenLocalhost(5001, o => o.Protocols =
                        HttpProtocols.Http2);

                    // ADDED THIS LINE to fix the problem
                    options.ListenLocalhost(11837, o => o.Protocols =
                        HttpProtocols.Http1);
                });
                webBuilder.UseStartup<Startup>();
            });
    }
}

Thnx

Solution 2:[2]

Another solution is configure Kestrel parameter on appsettings.json. It work with .Net Core 3.1 too.

Edit appsettings.json and set Endpoints with WebApi and gRPC with your custom name. You don't need change the Program.cs.

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "WebApi": {
        "Url": "http://localhost:5001",
        "Protocols": "Http1"
      },
      "gRPC": {
        "Url": "http://localhost:11837",
        "Protocols": "Http2"
      }
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Changemyminds