A new experiment: using grpc web to call. Net grpc service from browser

Time:2020-6-17

A new experiment: using grpc web to call. Net grpc service from browser

Today, I’d like to translate an article from ASP.NET James Newton king, chief development engineer, published a blog a few days ago, which brought an experimental product, grpc web. You can click the discussion post at the end of the article to give relevant feedback. I’ll give a link to the original at the end of the article. All translations are as follows:
I’m glad to announce experimental support for grpc web through. Net. Grpc web allows you to call grpc from a browser based application, such as a JavaScript spa or razor webassembly application.

. net’s grpc web promises to bring many of the outstanding features of grpc into browser applications:

  • Strong type code generation client
  • Compact protobuf message
  • Service flow

What is grpc Web

The grpc http / 2 specification cannot be implemented in the browser because there is no browser API that has sufficient fine-grained control over HTTP requests. Grpc web solves this problem by being compatible with HTTP / 1.1 and HTTP / 2.

Grpc web is not a new technology. There is already a stable grpc web JavaScript client and a proxy for converting between grpc and grpc web. New experimental packages allow ASP.NET Core grpc application supportNoGrpc web with proxy and allows. Net core grpc clients to call grpc web services. (perfect for blazor webassembly applications!)

New scenarios using grpc Web

  • Call from browser ASP.NET Core grpc application

    – the browser API cannot call grpc http / 2. Grpc web provides a compatible alternative.

    • JavaScript SPA
    • . net razor web assembly application
  • Hosted in IIS and azure app service ASP.NET Core grpc application– some servers, such as IIS and azure app services, are currently unable to host grpc services. While actively exploring this issue, grpc web provides an interesting alternative that can be used in every environment today.

  • Call grpc from a non. Net core platform– some. Net platformsHttpClientHttp / 2 is not supported. Grpc web can be used to call grpc services on these platforms (for example, blazor webassembly, xamarin).

Note that the performance cost of grpc web is low, and it no longer supports two grpc functions: client flow and two-way flow. (still supports server streaming!)

Server gRPC-Web description

If you are new to grpc in. Net, here is a simple getting started guide.

Grpc web does not need to make any changes to the service, the only change is to start the configuration. To be in ASP.NET Enable grpc web in core grpc service, please addGrpc.AspNetCore.WebA reference to the package. By addingAddGrpcWeb(...)andUseGrpcWeb(), configure the application to use grpc Web:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddGrpcWeb();
}

public void Configure(IApplicationBuilder app)
{
    app.UseRouting();

    // Add gRPC-Web middleware after routing and before endpoints
    app.UseGrpcWeb();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGrpcService().EnableGrpcWeb();
    });
}

Calling grpc web from a browser may require some additional configuration, such as configuring the application to support CORS.

Client grpc web description

The grpc web client for JavaScript has instructions for setting up the grpc web client for use in the browser JavaScript spa.

Using. Net client to call grpc web is the same as general grpc. The only modification is the way to create channel. To enable grpc web, add theGrpc.Net.Client.WebA reference to the package. Configure channels to useGrpcWebHandler

// Configure a channel to use gRPC-Web
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
    {
        HttpClient = new HttpClient(handler)
    });

var client = Greeter.GreeterClient(channel);
var response = await client.SayHelloAsync(new GreeterRequest { Name = ".NET" });

To see the grpc web running. Net, take a moment to read the wonderful blog by Steve Sanderson, which uses grpc web in the blazor webassembly.

Try it now at ASP.NET Using grpc web in core

Preview package on nuget:

  • Grpc.AspNetCore.Web – add grpc web support to ASP.NET Core grpc service.
  • Grpc.Net.Client . Web – call grpc web endpoint on. Net

Documents for using grpc web with. Net core can be found here.

. net’s grpc web is an experimental project, not a stable release product. We want to test the effectiveness of our approach to grpc web and get feedback on whether it’s useful for. Net developers compared to the traditional approach of setting up grpc web through a proxy. You can https://github.com/grpc/grpc-dotnet Add usage feedback on to make sure we build things that developers like and can work.

thank you!

Original link: https://devblogs.microsoft.com/aspnet/grpc-web-experiment/

Original author: James Newton King

Translator: yilezhu