grpc的.net core使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了grpc的.net core使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3259字,纯文字阅读大概需要5分钟。
内容图文
公共数据
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();
}
}
}
输出结果:
原文:https://www.cnblogs.com/tangge/p/15221300.html
内容总结
以上是互联网集市为您收集整理的grpc的.net core使用全部内容,希望文章能够帮你解决grpc的.net core使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。