2

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?

Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22
  • I think you may want to ask yourself if hosting them together even makes sense. The use cases and access patterns likely would likely lead you to separate physical endpoints (port and microservice/project). And use a reverse proxy to map them to the physical. – Kit Feb 25 '23 at 06:37
  • @Kit Why not make the app able to respond through different types of APIs when logic and data are the same? – Hadi Samadzad Mar 01 '23 at 21:43
  • That's certainly ok. You just need to make the architectural decisions that fit. If the usage patterns for both are the same, your approach is reasonable. That said, not sure why it's not working for you. When you say the "gRPC client doesn't work", that's a bit vague. Have you tried `Http1AndHttp2` for the `Protocols` property? – Kit Mar 02 '23 at 02:28
  • Actually, I think I should set two different endpoint configs rather than using `Http2` for both endpoints, but don't know the syntax if it is possible through appsettings. – Hadi Samadzad Mar 02 '23 at 14:40

1 Answers1

3

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"
            }
        }
    }
}
dropbear
  • 1,510
  • 1
  • 12
  • 21