14

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

  • do you have a github repo or something with full code (like your .csproj, and `BootNodeService` class) – BurnsBA Oct 31 '19 at 20:46
  • @BurnsBA sure. I added the link at the bottom the body of my question. – just_a_programmer Oct 31 '19 at 20:55
  • Are you sure your web port is on port 8001? It looks like your project is configured for 11837/44380 (http/https). I cloned your project and hit f5, browser auto loads `https://localhost:44380/admin` and it shows "hello from admin" – BurnsBA Nov 01 '19 at 13:22
  • @BurnsBA ok. thank you. were you using macos by chance? – just_a_programmer Nov 01 '19 at 14:54
  • no macros. Visual Studio 2019, though it should build the same with any msbuild tools for dotnet core 3 – BurnsBA Nov 01 '19 at 15:28
  • @BurnsBA Solved because you made me realize something. I'll post a solution in a moment. I had to do some "trick" get gPRC to work on macos. I applied that same trick to the port for webapi and it works now. – just_a_programmer Nov 01 '19 at 15:59

2 Answers2

14

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

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"
      }
    }
  }
}
Changemyminds
  • 1,147
  • 9
  • 25