New Experiment: Calling .NET gRPC Services from a Browser with gRPC-Web

We are glad to announce experimental support for gRPC-Web with .NET. gRPC-Web allows you to call gRPC from browser-based applications such as JavaScript SPA or Blazor WebAssembly applications.

gRPC-Web for .NET promises to bring many great gRPC features to browser applications:

  • Strictly Typed Code-Generated Clients
  • Compact Protobuf Posts
  • Server Streaming



What is gRPC-Web


It is not possible to implement the gRPC HTTP / 2 specification in a browser because there is no browser API with sufficient detailed 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 a stable  gRPC-Web JavaScript client , as well as a  proxy for translation between gRPC and gRPC-Web  for services. New experimental packages allow the ASP.NET Core gRPC application to support gRPC-Web without a proxy server and allow the .NET Core gRPC client to invoke gRPC-Web services. (Great for Blazor WebAssembly applications!)

New features with gRPC-Web


  • Call ASP.NET Core gRPC applications from a browser - Browser APIs cannot call gRPC HTTP / 2. gRPC-Web offers a compatible alternative.
    • JavaScript SPA
    • .NET Blazor Web Assembly Applications
  • Host ASP.NET Core gRPC applications in IIS and Azure Application Service. Some servers, such as IIS and Azure Application Service, are currently unable to host gRPC services. While they are actively working on it, gRPC-Web offers an interesting alternative that today works in any environment.
  • Call gRPC from platforms other than .NET Core. Some .NET platforms HttpClientdo not support HTTP / 2. gRPC-Web can be used to invoke gRPC services on these platforms (e.g. Blazor WebAssembly, Xamarin).

Note that gRPC-Web is low in performance and two gRPC features are no longer supported: client streaming and two-way streaming. (streaming to the server is still supported!)

GRPC-Web Server Instructions


If you haven't studied gRPC in .NET yet, here is a  simple tutorial so you can get started .

gRPC-Web does not require any changes to your services, the only modification is the initial configuration. To enable gRPC-Web with the ASP.NET Core gRPC service, add the link to the Grpc.AspNetCore.Web package . Configure the application to use gRPC-Web, adding AddGrpcWeb(...), and UseGrpcWeb()the startup file: Startup.cs

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

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

    //    gRPC-Web     
    app.UseGrpcWeb();

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

Calling gRPC-Web from a browser may require additional configuration, for example, configuring an application to support CORS.

GRPC-Web Client Instructions


The gRPC-Web JavaScript client has instructions  for setting up the gRPC-Web client for use in the JavaScript SPA browser.

Calling gRPC-Web with a .NET client is similar to regular gRPC, the only change is the way the channel is created. To enable gRPC-Web, add the link to the Grpc.Net.Client.Web package . Configure the channel to use GrpcWebHandler:

//     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 gRPC-Web with .NET in action, read the coolest blog post of our colleague who used  gRPC-Web in Blazor WebAssembly .

Try gRPC-Web with ASP.NET Core today


Preview packages on NuGet:


You can find documentation on using gRPC-Web with .NET Core  here .

Note: while gRPC-Web for .NET is only a pilot project, not a supported product.

Source: https://habr.com/ru/post/undefined/


All Articles