公共数据
InMemoryData.cs
public class InMemoryData
{
public static List<Employee> Employees = new List<Employee>
{
new Employee
{
Id = 10001,
No= 2000,
FirstName = "张",
LastName = "飞",
Salary = 3500
},
new Employee
{
Id = 10002,
No= 1990,
FirstName = "刘",
LastName = "备",
Salary = 5500
},
new Employee
{
Id = 10003,
No= 1995,
FirstName = "关",
LastName = "羽",
Salary = 4500
},
};
}
一元消息
Service
安装包
Grpc.AspNetCore
创建proto
创建一个文件夹Protos, 并且创建一个文件
Messages.proto
syntax = "proto3";
option csharp_namespace = "GrpcServer.Web.Protos";
message Employee{
int32 id = 1;
int32 no = 2;
string firstName = 3;
string lastName = 4;
float salary = 5;
}
message GetByNoRequest{
int32 no =1;
}
message EmployeeRequest{
Employee employee = 1;
}
message EmployeeResponse{
Employee employee = 1;
}
message GetAllRequest{}
message AddPhotoRequest{
bytes data = 1;
}
message AddPhotoResponse{
bool isOk = 1;
}
service EmployeeService{
rpc GetByNo (GetByNoRequest) returns(EmployeeResponse);
rpc GetAll (GetAllRequest) returns(stream EmployeeResponse);
rpc AddPhoto (stream AddPhotoRequest) returns (AddPhotoResponse);
rpc Save (EmployeeRequest) returns (EmployeeResponse);
rpc SaveAll (stream EmployeeRequest) returns (stream EmployeeRequest);
}
然后设置属性。
设置后生成一次应用程序生成。
编写 Service
using Grpc.Core;
using GrpcServer.Web.Data;
using GrpcServer.Web.Protos;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GrpcServer.Web.Services
{
public class MyEmployeeService : EmployeeService.EmployeeServiceBase
{
public readonly ILogger<MyEmployeeService> _logger;
public MyEmployeeService(ILogger<MyEmployeeService> logger)
{
_logger = logger;
}
public override Task<EmployeeResponse> GetByNo(GetByNoRequest request, ServerCallContext context)
{
var employee = InMemoryData.Employees
.SingleOrDefault(x => x.No == request.No);
//client 发送的元数据(Metadata)
var metadata = context.RequestHeaders;
foreach (var item in metadata)
{
_logger.LogInformation($"Metadata RequestHeaders: {item.Key}:{item.Value}");
}
if (employee is not null)
{
var response = new EmployeeResponse
{
Employee = employee
};
return Task.FromResult(response);
}
throw new Exception(message: $"employee is null.");
//return base.GetByNo(request, context);
}
}
}
配置Startup.cs
services.AddGrpc(); //service 注册
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<MyEmployeeService>(); //将传入请求映射到指定的 TService 类型。
});
Client
安装包
Google.Protobuf Potobuf 序列化协议的包
Grpc.Net.Client 客户端的包
Grpc.Tools 命令行工具
编写 Client
using Grpc.Core;
using Grpc.Net.Client;
using GrpcServer.Web.Protos;
using System;
using System.Threading.Tasks;
namespace GrpcClient
{
class Program
{
static async Task Main(string[] args)
{
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new EmployeeService.EmployeeServiceClient(channel);
//发送给sevice的元数据
var header = new Metadata
{
{ "role","Admin"},
{ "username","testAdmin"}
};
var response = await client.GetByNoAsync(new GetByNoRequest
{
No = 1995
}, header);
Console.WriteLine($"message:{response}");
Console.WriteLine("Press key to exit.");
Console.ReadLine();
}
}
}
输出结果: