Adds gRPC-Web support to .NET client for gRPC
$ dotnet add package Grpc.Net.Client.WebThe .NET gRPC client can be configured to make gRPC-Web calls. This is useful for Blazor WebAssembly apps, which are hosted in the browser and have the same HTTP limitations of JavaScript code. Calling gRPC-Web with a .NET client is the same as HTTP/2 gRPC. The only modification is how the channel is created.
To use gRPC-Web:
GrpcWebHandler:var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
{
HttpHandler = new GrpcWebHandler(new HttpClientHandler())
});
var client = new Greeter.GreeterClient(channel);
var response = await client.SayHelloAsync(new HelloRequest { Name = ".NET" });
The preceding code:
GrpcWebHandler has the following configuration options:
HttpMessageHandler that makes the gRPC HTTP request, for example, HttpClientHandler.Content-Type is application/grpc-web or application/grpc-web-text.
GrpcWebMode.GrpcWeb configures content to be sent without encoding. Default value.GrpcWebMode.GrpcWebText configures content to be base64 encoded. Required for server streaming calls in browsers.GrpcChannelOptions.HttpVersion and GrpcChannelOptions.HttpVersionPolicy can be used to configure the HTTP protocol version.
Traditional gRPC over HTTP/2 supports streaming in all directions. gRPC-Web offers limited support for streaming:
When using gRPC-Web, we only recommend the use of unary methods and server streaming methods.