How do you let a browser call an ASP.NET Core gRPC price-quote service through gRPC-Web, and what must change in the app's configuration?
answer
- Grpc.AspNetCore.Web package
- UseGrpcWeb after routing, before endpoints
- EnableGrpcWeb or DefaultEnabled = true
- Kestrel Http1AndHttp2, not Http2 only
- CORS WithExposedHeaders Grpc-Status
basics
~10 sAdd Grpc.AspNetCore.Web, call app.UseGrpcWeb(), opt the service in with EnableGrpcWeb() because GrpcWebOptions.DefaultEnabled is false, allow HTTP/1.1 on the Kestrel endpoint, and for cross-origin pages add a CORS policy exposing the grpc-status headers.
solid answer
~40 sBrowsers cannot make plain gRPC calls, so the service has to speak gRPC-Web. In ASP.NET Core that is the `Grpc.AspNetCore.Web` middleware: `app.UseGrpcWeb()` after routing and before the endpoints, plus `app.MapGrpcService<PriceQuoteService>().EnableGrpcWeb()`. `GrpcWebOptions.DefaultEnabled` is false, so an endpoint without `EnableGrpcWeb()` (or `[EnableGrpcWeb]`) is not translated; `UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true })` opts every service in. gRPC-Web works over HTTP/1.1, while `dotnet new grpc` limits Kestrel to `Http2`, so the endpoint needs `Http1AndHttp2`, which on one port requires TLS. A page from another origin needs `AddCors` with `WithExposedHeaders("Grpc-Status", "Grpc-Message", ...)`, `UseCors()` and `RequireCors(...)` on the mapped service. Back-end callers keep using plain gRPC on the same endpoints.
code
csharp · 17 linesbuilder.Services.AddGrpc();
builder.Services.AddCors(o => o.AddPolicy("PricingDashboard", p => p
.WithOrigins("https://dashboard.example")
.AllowAnyHeader().AllowAnyMethod()
.WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding",
"Grpc-Accept-Encoding", "Grpc-Status-Details-Bin")));
var app = builder.Build();
app.UseGrpcWeb(); // after routing, before endpoints
app.UseCors();
app.MapGrpcService<PriceQuoteService>()
.EnableGrpcWeb() // DefaultEnabled is false
.RequireCors("PricingDashboard");
app.Run();go deeper
Recall that browsers use gRPC-Web, that ASP.NET Core adds it with UseGrpcWeb, and that each service must opt in.
Explain DefaultEnabled, EnableGrpcWeb and the 415 a non-opted endpoint returns, plus why Kestrel must allow HTTP/1.1.
Configure gRPC-Web, CORS exposed headers, TLS for mixed protocols and authorization so one service safely serves back ends and browsers.
Decide between in-app gRPC-Web, a translating proxy and JSON transcoding for browser access across a fleet of gRPC services.
## Why a translation layer The internal price-quote service is called by other back ends over HTTP/2 gRPC, and now a pricing dashboard in the browser needs it too. Browser APIs do not give a page the control over HTTP/2 framing and trailers that gRPC needs, so the browser speaks **gRPC-Web**, a variant that carries the status in the body and works over HTTP/1.1 or HTTP/2. ASP.NET Core can accept it directly with `Grpc.AspNetCore.Web`, without a separate proxy. ## Server changes 1. Reference `Grpc.AspNetCore.Web`. 2. Add the middleware: `app.UseGrpcWeb();`. It must see the selected endpoint, so it goes after routing and before the endpoints execute. In a `WebApplication`, routing runs first unless you call `UseRouting` yourself; place `UseGrpcWeb` after any explicit `UseRouting`. 3. Opt the service in with `app.MapGrpcService<PriceQuoteService>().EnableGrpcWeb();`, or put `[EnableGrpcWeb]` on the service class. `GrpcWebOptions.DefaultEnabled` is **false**. A gRPC-Web request to an endpoint that did not opt in passes through untranslated, and the gRPC handler rejects its `application/grpc-web` content type with **415**. To enable every service, use `app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true })` and opt individual endpoints out with `DisableGrpcWeb()`. The middleware rewrites a recognised gRPC-Web request so the normal gRPC handler can run it, and converts the response back. The same `PriceQuoteService` serves both kinds of caller. ## Kestrel protocols | Setting | Plain gRPC | gRPC-Web over HTTP/1.1 | |---|---|---| | `Http2` (the `grpc` template default) | works | rejected: no HTTP/1.1 | | `Http1AndHttp2` | works | works | HTTP/1.1 and HTTP/2 on the same port require TLS, because the protocol is negotiated in the TLS handshake (ALPN). Without TLS, use separate endpoints. Kestrel endpoint configuration in detail belongs to hosting. ## Cross-origin pages and CORS If the dashboard is served from another origin, the browser applies CORS. A gRPC-Web client must read gRPC's own response headers to learn the call's outcome, so they have to be exposed: - `builder.Services.AddCors(o => o.AddPolicy("PricingDashboard", p => p.WithOrigins("https://dashboard.example").AllowAnyHeader().AllowAnyMethod().WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding", "Grpc-Status-Details-Bin")));` - `app.UseCors();` between routing and endpoints, next to `UseGrpcWeb()`. - `.RequireCors("PricingDashboard")` on the mapped service. Without CORS middleware, a preflight `OPTIONS` request reaching the gRPC endpoint is answered with 405 and a log message that CORS may not be configured correctly. ## Clients and limits - .NET clients that cannot use HTTP/2, such as Blazor WebAssembly, use `GrpcWebHandler` from `Grpc.Net.Client.Web` as the channel's handler. `GrpcWebMode.GrpcWeb` is the default; `GrpcWebMode.GrpcWebText` (base64) is needed for server streaming in browsers. - Through gRPC-Web, unary and server-streaming methods work; the streaming quote feed is fine, client and bidirectional streaming are not available to browsers. - Keep authentication on the endpoint (`RequireAuthorization()`), not in the middleware choice: both protocols reach the same authorization checks. ## Diagnosing a failing browser call 1. **415 Unsupported Media Type:** the endpoint did not opt in; add `EnableGrpcWeb()` or enable it by default. 2. **405 on a preflight `OPTIONS`:** CORS middleware or the policy is missing, so the preflight reached the gRPC endpoint. 3. **Connection resets or protocol errors over HTTP/1.1:** the Kestrel endpoint still allows only `Http2`. 4. **The call seems to succeed but the client reports an unknown status:** `Grpc-Status` and `Grpc-Message` were not exposed through CORS, so the script cannot read them.
- UseGrpcWeb() is in place, but dashboard calls fail with 415. What is the likely cause?The service endpoint never opted in. With `GrpcWebOptions.DefaultEnabled` false and no `EnableGrpcWeb()` or `[EnableGrpcWeb]`, the middleware leaves the request alone and the gRPC handler rejects the `application/grpc-web` content type with 415. Add `EnableGrpcWeb()` to the mapping or enable it by default.
- When would you rather put a translating proxy in front of the service than use Grpc.AspNetCore.Web?When the environment already routes traffic through a proxy that translates gRPC-Web to gRPC, keeping the translation there avoids configuring it in every service. For a single ASP.NET Core service, the middleware is the simpler choice and needs no extra component.
- UseGrpcWeb() alone enables gRPC-Web for every mapped gRPC service.
- gRPC-Web needs a separate service class next to the normal gRPC service.
- The grpc template's Http2-only Kestrel setting is fine for gRPC-Web over HTTP/1.1.
- AllowAnyHeader() also exposes Grpc-Status to the browser, so WithExposedHeaders is unnecessary.
- gRPC-Web replaces plain gRPC, so back-end callers must switch to gRPC-Web too.