Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 916
just_a_programmer Avatar asked Oct 31 '19 19:10

just_a_programmer


People also ask

Can gRPC call REST API?

Yes, it is possible. You can make calls to other APIs and services from your own gRPC service code. Just let your client make a call to your gRPC service. Then, your service makes a REST call to external API (possibly using arguments from client request to your gRPC service) and processes it.

What is difference between Web API and .NET core?

In ASP.NET Core, there's no longer any distinction between MVC and Web APIs. There's only ASP.NET MVC, which includes support for view-based scenarios, API endpoints, and Razor Pages (and other variations like health checks and SignalR). In addition to being consistent and unified within ASP.NET Core, APIs built in .


1 Answers

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

like image 81
just_a_programmer Avatar answered Sep 17 '22 19:09

just_a_programmer