asp.netasp.net-web-apigrpcappsettingsgrpc-dotnet

Running REST API and gRPC simultanously using ASP.NET Core


I am trying to run a gRPC server and client using ASP.NET Core 6 side-by-side REST API on a specific port like 5000. I mean, I want to use localhost:5000, but after checking the dotnet's gRPC template I found that the Http2 protocol should be set for Kestrel Endpoints in appsettings.json like this:

{
  "urls": "http://localhost:5000",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }
}

By doing this I encounter an issue and my REST APIs don't work anymore I guess it's because of the Http2 protocol provided to the Kestrel endpoints because when I remove the provided protocol APIs work normally; instead, the gRPC client doesn't work. Is there any way to run both endpoints side-by-side?


Solution

  • It's possible to configure gRPC and Web API to use different ports with the following appsettings.json configuration:

    {
        "Kestrel": {
            "Endpoints": {
                "gRPC": {
                    "Url": "https://localhost:5000",
                    "Protocols": "Http2"
                },
                "WebApi": {
                    "Url": "http://localhost:5001",
                    "Protocols": "Http1"
                }
            }
        }
    }