asp.net core 3.0中Grpc.AspNetCore.Server带来了什么

Grpc.AspNetCore.Server带来了什么

dotnet core 3.0在公布的时候有一个重要的feature, 那就是支持gRPC.
gRPC作为一个nuget包早就能工作在Net Core下面.
那dotnet core 3.0中对gRPC的支持到底实现了哪些功能呢?

在.net core 2.2中Host Grpc服务器

参考上一篇文章, 使用protoc或者Visual Studio编辑生成了C#的服务器端代码.
实现服务器端具体的逻辑:

class GreeterImpl : Greeter.GreeterBase
{
    // Server side handler of the SayHello RPC
    public override Task SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
    }

    // Server side handler for the SayHelloAgain RPC
    public override Task SayHelloAgain(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello again " + request.Name });
    }
}

在Console项目中Host gRPC服务:

public static void Main(string[] args)
{
    Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);

    var client = new Greeter.GreeterClient(channel);
    String user = "you";

    var reply = client.SayHello(new HelloRequest { Name = user });
    Console.WriteLine("Greeting: " + reply.Message);

    var secondReply = client.SayHelloAgain(new HelloRequest { Name = user });
    Console.WriteLine("Greeting: " + secondReply.Message);

    channel.ShutdownAsync().Wait();
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

Console项目可以考虑作为Windows服务.
或者在asp.net core中采用IHostedService方式Host. 可参考: Background tasks with hosted services in ASP.NET Core

在.net core 3.0中使用Grpc.AspNetCore.Server Host gRPC

在.net core 3.0中可以直接支持gRPC, 项目的创建模版中可以直接选择gRPC, 生成项目的startup.cs中可以找到看到grpc已经作为默认的middleware添加:

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

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

这也意味者该项目中还可以添加其他的中间件比如说MVC Controller, 让gRPC可以与restful api可以工作在同一个项目中.

Grpc.AspNetCore.Server带来了哪些功能

添加了gRPC middleware之后, asp.net core默认的功能都能够使用.

DI与Logging

asp.net core中最核心的功能DI可以在gRPC服务中使用.

public class GreeterService : Greeter.GreeterBase
{
    public GreeterService(ILogger logger)
    {
    }
}

HttpContext支持

gRPC是基于http/2的, 在ServerCallContext中可以访问到metadata, 比如说method, host, header等等http的基本信息.
在Grpc.AspNetCore.Server中对ServerCallContext进行了扩展, 可以直接转换到HttpContext, 这样就可以访问session, user ClaimsPrincipal这些基本的数据.

public class GreeterService : Greeter.GreeterBase
{
    public override Task
        SayHello(HelloRequest request, ServerCallContext context)
    {
        var httpContext = context.GetHttpContext();

        return Task.FromResult(new HelloReply
        {
            Message = "Using https: " + httpContext.Request.IsHttps
        });
    }
}

你可能感兴趣的:(asp.net core 3.0中Grpc.AspNetCore.Server带来了什么)