A new experiment: Using .NET gRPC gRPC-Web service call from the browser

Today to a translation by the ASP.NET chief development engineer James Newton-King a few days ago published a blog, the paper brought an experimental product gRPC-Web. You can click on the text at the end of the discussion thread related feedback. I will give a description link at the end of the article. All translations are as follows:
I am pleased to announced experimental support for gRPC-Web by .NET. gRPC-Web allows calls from gRPC browser-based applications (such as JavaScript SPA or Blazor WebAssembly application).

.NET's gRPC-Web is committed to gRPC many outstanding features introduced browser application:

  • Strongly typed code generation client
  • Compact Protobuf news
  • Service Flow

What is gRPC-Web

Can not be achieved gRPC HTTP / 2 specifications in the browser, the browser API because no HTTP requests can be sufficiently fine-grained control. gRPC-Web to solve this problem by compatible with HTTP / 1.1 and HTTP / 2.

gRPC-Web is not a new technology. There has been a steady gRPC-Web JavaScript client , and a proxy used to convert between gRPC and Web-gRPC . New experimental package allows ASP.NET Core gRPC application supports not gRPC-Web with proxy and allows .NET Core gRPC client calls gRPC-Web services. (Very suitable Blazor WebAssembly application!)

GRPC-Web using a new scene

  • Call ASP.NET Core gRPC application from the browser

    - browser API can not call gRPC HTTP / 2. gRPC-Web provides an alternative to a compatible.

    • JavaScript SPA
    • .NET Blazor Web Assembly应用
  • 在IIS和Azure App Service中托管ASP.NET Core gRPC应用程序 –某些服务器(例如IIS和Azure App Service)当前无法托管gRPC服务。在积极研究这一问题的同时,gRPC-Web提供了一种有趣的替代方案,可在当今的每个环境中使用。

  • 从非.NET Core平台调用gRPC –一些.NET平台HttpClient不支持HTTP / 2。gRPC-Web可用于在这些平台(例如Blazor WebAssembly,Xamarin)上调用gRPC服务。

请注意,gRPC-Web的性能成本较低,并且不再支持两个gRPC功能:客户端流和双向流。(仍然支持服务端流!)

服务端gRPC-Web说明

如果您是.NET中gRPC的新手,那么这里有一个简单的入门指南

gRPC-Web不需要对服务进行任何更改,唯一的修改是启动配置。要在ASP.NET Core gRPC服务中启用gRPC-Web,请添加对Grpc.AspNetCore.Web包的引用。通过在启动文件中添加AddGrpcWeb(...)UseGrpcWeb(),将应用程序配置为使用gRPC-Web :

Startup.cs

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

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

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

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

从浏览器调用gRPC-Web可能需要一些其他配置,例如将应用程序配置为支持CORS。

客户端gRPC-Web说明

JavaScript的gRPC的Web客户端有关于设置gRPC Web客户端以在浏览器JavaScript SPAs中使用的说明

使用.NET客户端调用gRPC-Web与常规gRPC相同,唯一的修改是创建通道的方式。要启用gRPC-Web,请添加对Grpc.Net.Client.Web包的引用。配置通道以使用GrpcWebHandler

// 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" });

要查看运行.NET的gRPC-Web,请花点时间阅读由Steve Sanderson撰写的精彩博客,该博客在Blazor WebAssembly中使用gRPC-Web

立即尝试在ASP.NET Core中使用gRPC-Web

NuGet上的预览包:

可以在此处找到将gRPC-Web与.NET Core一起使用的文档。

.NET的gRPC-Web是一个实验性项目,而不是稳定发布的产品。我们想测试一下我们实现gRPC-Web的方法是否有效,并获得反馈,与通过代理设置gRPC-Web的传统方法相比,该方法对.NET开发人员是否有用。大家可以在https://github.com/grpc/grpc-dotnet上添加使用反馈,以确保我们构建出开发人员喜欢并能发挥作用的东西。

谢谢!

原文链接:https://devblogs.microsoft.com/aspnet/grpc-web-experiment/

原文作者:James Newton-King

翻译作者:依乐祝

Guess you like

Origin www.cnblogs.com/yilezhu/p/12313870.html