News

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.

The new functinality is key to Microsoft's ASP.NET Core and Blazor initiatives, with gRPC acting 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, the web development component. Blazor has gained attention for its ability to let .NET coders do web development with C# or F# instead of JavaScript, which is used in just about every other web-dev scheme.

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.

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

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.

"gRPC-Web is not a new technology. There is a stable gRPC-Web JavaScript client, and a proxy for translating between gRPC and gRPC-Web for services. The new experimental packages allow an ASP.NET Core gRPC app to support gRPC-Web without a proxy, and allow the .NET Core gRPC client to call gRPC-Web services. (great for Blazor WebAssembly apps!)"

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:
    • 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 – 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."

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

Subscribe on YouTube