惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
V
V2EX
博客园 - 【当耐特】
Vercel News
Vercel News
雷峰网
雷峰网
爱范儿
爱范儿
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
NISL@THU
NISL@THU
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
P
Proofpoint News Feed
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
I
InfoQ
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 最新话题
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
V
Visual Studio Blog
AI
AI
Schneier on Security
Schneier on Security
B
Blog RSS Feed
T
Tor Project blog
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
Arctic Wolf
Webroot Blog
Webroot Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - DotNet菜园

gRPC入门学习之旅(十) gRPC入门学习之旅(九) gRPC入门学习之旅目录 gRPC入门学习之旅(八) gRPC入门学习之旅(七) gRPC入门学习之旅(六) gRPC入门学习之旅(五) gRPC入门学习之旅(四) gRPC入门学习之旅(二) gRPC入门学习之旅(一) WPF入门教程系列三十 ——DataGrid验证 WPF入门教程系列二十九 ——DataGrid使用示例MVVM模式(7) WPF入门教程系列二十八 ——DataGrid使用示例MVVM模式(6) WPF入门教程系列二十八 ——DataGrid使用示例MVVM模式(5) WPF入门教程系列二十七 ——DataGrid使用示例MVVM模式(4) WPF入门教程系列二十六——DataGrid使用示例(3) WPF入门教程系列二十五——DataGrid使用示例(2) WPF入门教程系列目录 WPF入门教程系列二十四——DataGrid使用示例(1)
gRPC入门学习之旅(三)
DotNet菜园 · 2024-03-23 · via 博客园 - DotNet菜园

gRPC入门学习之旅(一)

2.3、创建自定义服务

除上面的模板中自带的一个gRPC服务之后,我们再创建一个自己的服务,我将创建一个用户信息gRPC服务,主要功能有三个,登录、获取用户信息、修改用户信息。

  • 创建UserInfo.proto协议文件
  1. 在“解决方案资源管理器”中,使用鼠标左键选中“Protos”文件夹,然后在菜单栏上,依次选择“添加-->新建项”。如下图。

 

  1. 在“添加新项”对话框中,选择“ASP.NET Core-->常规”节点,然后选择“协议缓冲区文件”项。
  2. 在“名称”文本框中,输入 UserInfo.proto,然后选择“添加”按钮。如下图。

 

   4.Visual Studio 2022会在Protos目录中添加一个UserInfo.proto协议文件。如下图。

 

5.这个UserInfo.proto文件中的代码如下:

syntax = "proto3";

 

option csharp_namespace = "Demo.GrpcService.Protos";

//包名

package user;

 

//服务名称

service UserInfo{

//方法名称

rpc GetUserInfo (UserInfoRequest) returns (UserInfoResult);

rpc Login(UserInfoRequest) returns(UserInfoResult);

rpc Save(UserInfoRequest) returns (UserInfoResult);

}

//请求的参数对象

message UserInfoRequest{

string UserName = 1;

 string Password=2;

}

//请求响应的对象
message UserInfoResult{
string UserName = 1;
 string Password=2;

int32  Age = 3;

string Tel=4;

string Name=5;

int32 Sex=6;

string City=7;

bool IsLogin=8;

}
  • 添加userinfo.proto协议文件的关联配置

6.在“解决方案资源管理器”中,使用鼠标左键选中项目名称“Demo.GrpcService”,然后使用鼠标双击这个名称,Visual Studio 2022将在文本编辑器中打开项目文件。 如下图。

 

7. 在文本编辑器中找到ItemGroup节点,在ItemGroup节点中增加如下配置:如下图。

 

 <ItemGroup>

    <Protobuf Include="Protos\greet.proto" GrpcServices="Server" />

      <Protobuf Include="Protos\UserInfo.proto" GrpcServices="Server" />

  </ItemGroup>

8.在“解决方案资源管理器”中,使用鼠标左键选中项目名称“Demo.GrpcService”,然后单击鼠标右键,在弹出的快捷菜单中选择“重新生成”菜单项。如下图。

9.我们打开“文件资源管理器”,进入到Demo.Grpc.Service\obj\Debug\net7.0\Protos目录,发现此时目录下比刚才多了2个.cs文件,就是我们自己定义的UserInfo.proto协议文件对应的类文件,如下图所示: