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 at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube