Move Over, JSON, Here’s gRPC-Web for .NET (& Blazor)
Now a fully supported component of the grpc-dotnet project, it allows for browser-based use of gRPC, an open standard based on HTTP/2 — which doesn’t work well on some .NET components — and other modern technologies.
It can act as a substitute for Windows Communication Foundation (WCF) technology that didn’t make the transition from the old, Windows-only .NET Framework to the new open-source, crosss-platform “Core” direction that encompasses .NET Core along with ASP.NET Core.
What’s more, it serves as a substitute for JSON, with better performance.
“We’ve worked with the Blazor team to make gRPC-Web a great end-to-end developer experience when used in Blazor WebAssembly apps,” Microsoft’s James Newton-King, principal software engineer, ASP.NET, said in a June 16 blog post. “Not only will gRPC tooling automatically generate strongly typed clients for you to call gRPC services from your Blazor app, but gRPC offers significant performance benefits over JSON.”
gRPC-Web for .NET was needed for Blazor and other projects because browsers don’t have APIs to to adequately control gRPC HTTP/2 requests.
“gRPC-Web is a standardized protocol that solves this problem and makes gRPC usable in the browser. gRPC-Web brings many of gRPC’s great features, like small binary messages and contract-first APIs, to modern browser apps.”
Those features are now production-ready, allowing for new coding scenarios 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 — HTTP/2 is not supported by HttpClient on all .NET platforms. gRPC-Web can be used to call gRPC services from Blazor and Xamarin.
Blazor guru Steve Sanderson early this year detailed how gRPC fits into the company’s plans for Blazor, about the same time Microsoft officially introduced ASP.NET Core experimental support for gRPC-Web. Since then, using feedback from early adopters, Microsoft has improved the product now generally available for production use.
Resources for more information include:
- Use gRPC in browser apps documentation
- Create a gRPC client and server in ASP.NET Core
- A browser example that shows how to use gRPC-Web with ASP.NET Core to call a gRPC service from a browser
- A “Using gRPC-Web with Blazor WebAssembly” blog post by Steven Sanderson
- Two packages available on NuGet:
- Grpc.AspNetCore.Web – Add gRPC-Web support to an ASP.NET Core gRPC service
- Grpc.Net.Client.Web – Call gRPC-Web endpoints from .NET
- The “gRPC for .NET” repository on GitHub
gRPC also was a focal point of Visual Studio Magazine writer Peter Vogel, who wrote the following articles for more information.
- What Are gRPC Web Services and When Should I Use Them
- How to Design Messages for gRPC Services in .NET Core
- Defining Your First gRPC Service in an ASP.NET Core 3.0 Project
- Writing the Code for a gRPC Service and Client in ASP.NET Core 3.0
- Don’t Do It All Yourself: Exploiting gRPC Well Known Types in .NET Core
- Reusing and Recycling Data Structures in gRPC Services in .NET Core
- Implementing State in .NET Core gRPC Messages with oneof
- .NET Core: Writing Really Obvious Code with Enumerated Values in gRPC Web Services