News

Move Over, JSON, Here's gRPC-Web for .NET (& Blazor)

Microsoft officially released gRPC-Web for .NET, aiming the RPC-based (Remote Procedure Call) framework at browser-based apps, including those built with Blazor, the red-hot project that allows for creating browser/web apps with C# instead of JavaScript.

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."

 Data transferred on the Blazor default template app's Fetch page is halved when gRPC is used instead of JSON. Data size is reduced from 627 bytes down to 309 bytes.
[Click on image for larger view.] Data transferred on the Blazor default template app's Fetch page is halved when gRPC is used instead of JSON. Data size is reduced from 627 bytes down to 309 bytes. (source: Microsoft).

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.
    • JavaScript SPAs
    • .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.

Blazor gRPC
[Click on image for larger view.] Early documentation for Blazor gRPC (source: Microsoft).

Resources for more information include:

gRPC also was a focal point of Visual Studio Magazine writer Peter Vogel, who wrote the following articles for more information.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus

Featured

Subscribe on YouTube