zoukankan      html  css  js  c++  java
  • 使用 ASP.NET Core 的 gRPC 服务

    将 gRPC 服务添加到 ASP.NET Core 应用

    gRPC 需要gRPC包。

    配置 gRPC

    在 Startup.cs 中:

    • gRPC 是通过AddGrpc方法启用的。
    • 每个 gRPC 服务通过MapGrpcService方法添加到路由管道。
    C#
     
     
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseRouting();
    
            app.UseEndpoints(endpoints =>
            {
                // Communication with gRPC endpoints must be made through a gRPC client.
                // To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909
                endpoints.MapGrpcService<GreeterService>();
            });
        }
    }
    

    ASP.NET Core 中间件和功能共享路由管道, 因此可以将应用配置为提供其他请求处理程序。 其他请求处理程序 (如 MVC 控制器) 与已配置的 gRPC 服务并行工作。

    配置 Kestrel

    Kestrel gRPC 终结点:

    HTTP/2

    gRPC 要求 HTTP/2。 gRPC for ASP.NET Core 验证HttpRequestHTTP/2

    在大多数现代操作系统上,Kestrel支持 HTTP/2 。 默认情况下,Kestrel 终结点配置为支持 HTTP/1.1 和 HTTP/2 连接。

    TLS

    用于 gRPC 的 Kestrel 终结点应使用 TLS 进行保护。 在开发中,将在存在 ASP.NET Core 开发证书https://localhost:5001时,自动创建一个使用 TLS 保护的终结点。 不需要配置。 https前缀验证 Kestrel 终结点是否正在使用 TLS。

    在生产环境中,必须显式配置 TLS。 在下面的appsettings示例中,提供了使用 TLS 保护的 HTTP/2 终结点:

    JSON
     
    {
      "Kestrel": {
        "Endpoints": {
          "HttpsInlineCertFile": {
            "Url": "https://localhost:5001",
            "Protocols": "Http2",
            "Certificate": {
              "Path": "<path to .pfx file>",
              "Password": "<certificate password>"
            }
          }
        }
      }
    }
    

    或者,可以在Program.cs中配置 Kestrel 终结点:

    C#
     
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(options =>
                {
                    options.Listen(IPAddress.Any, 5001, listenOptions =>
                    {
                        listenOptions.Protocols = HttpProtocols.Http2;
                        listenOptions.UseHttps("<path to .pfx file>", 
                            "<certificate password>");
                    });
                });
                webBuilder.UseStartup<Startup>();
            });
    

    协议协商

    TLS 用于保护通信的安全性。 当终结点支持多个协议时,TLS应用层协议协商(ALPN)握手用于协商客户端与服务器之间的连接协议。 此协商确定连接是使用 HTTP/1.1 还是 HTTP/2。

    如果在不使用 TLS 的情况下配置了 HTTP/2 终结点,则终结点的 ListenOptions.Protocols 必须设置为 HttpProtocols.Http2。 具有多个协议(例如)的终结HttpProtocols.Http1AndHttp2点不能使用 TLS,因为没有协商。 到不安全终结点的所有连接默认为 HTTP/1.1,并且 gRPC 调用失败。

    有关通过 Kestrel 启用 HTTP/2 和 TLS 的详细信息,请参阅Kestrel 终结点配置

     备注

    macOS 不支持 ASP.NET Core gRPC 及 TLS。 在 macOS 上成功运行 gRPC 服务需要其他配置。 有关详细信息,请参阅无法在 macOS 上启用 ASP.NET Core gRPC 应用

    与 ASP.NET Core Api 集成

    gRPC 服务对 ASP.NET Core 功能 (如依赖关系注入(DI) 和日志记录) 具有完全访问权限。 例如, 服务实现可以通过构造函数从 DI 容器解析记录器服务:

    C#
    public class GreeterService : Greeter.GreeterBase
    {
        public GreeterService(ILogger<GreeterService> logger)
        {
        }
    }
    

    默认情况下, gRPC 服务实现可以解析具有任意生存期 (单独、作用域或暂时性) 的其他 DI 服务。

    解析 gRPC 方法中的 HttpContext

    GRPC API 提供对某些 HTTP/2 消息数据 (如方法、主机、标头和尾部) 的访问权限。 通过传递给每ServerCallContext个 gRPC 方法的参数访问:

    C#
     
    public class GreeterService : Greeter.GreeterBase
    {
        public override Task<HelloReply> SayHello(
            HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name
            });
        }
    }
    

    ServerCallContext在所有 ASP.NET api 中都HttpContext不提供对的完全访问权限。 扩展方法提供对在 ASP.NET api 中HttpContext表示基础 HTTP/2 消息的完全访问权限: GetHttpContext

    C#
     
    public class GreeterService : Greeter.GreeterBase
    {
        public override Task<HelloReply> SayHello(
            HelloRequest request, ServerCallContext context)
        {
            var httpContext = context.GetHttpContext();
            var clientCertificate = httpContext.Connection.ClientCertificate;
    
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name + " from " + clientCertificate.Issuer
            });
        }
    }
    

    Azure 应用服务不支持 gRPC

  • 相关阅读:
    反向代理实例
    nginx常用命令和配置
    nginx的安装
    Can Live View boot up images acquired from 64bit OS evidence?
    What is the behavior of lnk files?
    EnCase v7 search hits in compound files?
    How to search compound files
    iOS 8.3 JB ready
    Sunglasses
    现代福尔摩斯
  • 原文地址:https://www.cnblogs.com/Javi/p/11752991.html
Copyright © 2011-2022 走看看