首页 > 解决方案 > Grpc.Net vs Grpc.Core:服务器端有什么区别?

问题描述

似乎 Grpc.Net 需要 ASP.NET Core 才能托管服务,而 Grpc.Core 不需要?有什么区别?哪个是首选?

网络

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

Grpc.Core

Server server = new Server
{
    Services = { RouteGuide.BindService(new RouteGuideImpl(features)) },
    Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
};
server.Start();

标签: asp.net-coregrpc-c#

解决方案


Grpc.Core是一个基于 gRPC 原生实现的库。它基本上是与本机库的绑定,可以直接将“原始”gRPC 用于客户端和服务器目的。

Grpc.AspNetCore是 gRPC 的托管重新实现,构建在 Kestrel 服务器堆栈之上。与原生实现相比,这使得 Grpc.AspNetCore 通常效率更高。它是为 ASP.NET Core 本身构建的,它还可以更好地集成到 ASP.NET Core 应用程序中,这还允许您与其他 ASP.NET Core 端点并排托管 gRPC 服务。相比之下,它可能不支持所有 gRPC 功能,并且也仅限于服务器端方面。


推荐阅读