Last week, Microsoft released a production-ready implementation of the gRPC-Web protocol for .NET. Initially supported as an experimental feature, the component is now part of the grpc-dotnet project. The new component makes gRPC usable in the browser, allowing web applications to communicate directly with gRPC services without using an HTTP server as a proxy.
Originally developed and used by Google, gRPC is a high-performance remote procedure call framework implemented over HTTP/2. However, since browsers do not directly expose the HTTP/2 framing, web applications can't use the gRPC implementation directly. gRPC-Web is a standardized protocol that solves this problem, making gRPC usable in the browser.
Earlier this year, Microsoft announced support for gRPC-Web as an experimental project. At the time, James Newton-King, principal software engineer for ASP.NET Core at Microsoft, stated that the project was not a committed product:
We want to test that our approach to implementing gRPC-Web and get feedback on whether this approach is useful to .NET developers compared to the traditional way of setting up gRPC-Web via a proxy.
The first gRPC-web implementation was released in 2018 as a JavaScript client library, allowing web applications to communicate directly with gRPC services without using an HTTP server as a proxy. This is possible through the creation of an end-to-end gRPC pipeline compatible with HTTP/1.1 and HTTP/2. The browser can then send regular HTTP requests, and a gRPC-Web proxy between the browser and the server allows the translation of requests and responses. Similarly to gRPC, gRPC-Web uses pre-defined contracts between the (web) client and backend gRPC services. Protocol Buffers are used for serializing and encoding messages (although it is possible to use other encoders).
(source: gRPC website)
With the new component, ASP.NET Core gRPC applications can be called directly from the browser by both .NET Blazor WebAssembly applications and JavaScript SPAs. They also offer an alternative to hosting ASP.NET Core gRPC applications in IIS and Azure App Service servers, since neither can currently host gRPC services.
There are, however, a few limitations when calling gRPC services with gRPC-Web from the browser: client streaming and bi-directional streaming calls are not supported, and calling gRPC services on a different domain requires CORS to be configured on the server. Also, the gRPC server must be configured to support gRPC-Web, or a third-party service proxy (such as Envoy) must translate the calls between the browser and the server.
With the new release, Microsoft updated the original project documentation and made a sample gRPC-Web application public. gRPC-WEB for .NET is available on NuGet as two separate packages: Grpc.AspNetCore.Web (adds gRPC-Web support to an ASP.NET Core gRPC service) and Grpc.Net.Client.Web (allows gRPC-Web endpoints to be called from .NET). The component requires .NET Core 3.1 SDK or later, and it can be used with Visual Studio 2019 (16.4 or later), Visual Studio Code, and Visual Studio for Mac (version 8.4 or later). More information on the advantages of using gRPC-Web can also be found here.