Experimental ASP.NET Core Support for gRPC-Web Allows Remote Procedure Calls from Browser
Microsoft officially introduced ASP.NET Core experimental support for gRPC-Web, which allows Google's remote procedure call (RPC) tech to work in browser-based web applications, something not previously possible.
gRPC-Web isn't new, and has been featured in previous presentations such as this one by Blazor guru Steve Sanderson in a recent online event, which showed how a Blazor WebAssembly app (the client-side Blazor component) could use gRPC. In that scenario, gRPC was used as a substitute for transporting JSON data over the wire.
However, in today's (Jan. 27) official introduction, gRPC-Web is touted for other benefits, including:
- Strongly-typed code-generated clients
- Compact Protobuf messages
- Server streaming
The gRPC web site on GitHub says:
gRPC-Web provides a JS client library that supports the same API as gRPC-Node to access a gRPC service. Due to browser limitation, the Web client library implements a different protocol than the native gRPC protocol. This protocol is designed to make it easy for a proxy to translate between the protocols as this is the most likely deployment model.
Today's post by James Newton-King, principal software engineer, ASP.NET, provides more intormation on those protocols.
"It is impossible to implement the gRPC HTTP/2 spec in the browser because there is no browser API with enough fine-grained control over HTTP requests," Newton-King explained. "gRPC-Web solves this problem by being compatible with HTTP/1.1 and HTTP/2.
That will result in new opportunities including:
Call ASP.NET Core gRPC apps from the browser – Browser APIs can't call gRPC HTTP/2. gRPC-Web offers a compatible alternative:
- .NET Blazor Web Assembly apps
- Host ASP.NET Core gRPC apps in IIS and Azure App Service – Some servers, such as IIS and Azure App Service, currently can't host gRPC services. While this is actively being worked on, gRPC-Web offers an interesting alternative that works in every environment today.
- Call gRPC from non-.NET Core platforms – Some .NET platforms HttpClient doesn't support HTTP/2. gRPC-Web can be used to call gRPC services on these platforms (e.g. Blazor WebAssembly, Xamarin).
However, some caveats to consider when using the new gRPC-Web include "a small performance cost" and the ending of support for client streaming and bi-directional streaming, though server streaming is still supported.
A wealth of information is available for the new initiative, including:
Also, Visual Studio Magazine writer Peter Vogel has been focusing on gRPC lately with these aritcles:
In the meantime, Newton-King and the rest of the ASP.NET Core team are dipping gRPC-Web's toes into the .NET development waters to see how it's received.
"gRPC-Web for .NET is an experimental project, not a committed product," he said. "We want to test that our approach to implementing gRPC-Web works, 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. Please add your feedback here or at the https://github.com/grpc/grpc-dotnet to ensure we build something that developers like and are productive with."
David Ramel is an editor and writer for Converge360.