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

推荐订阅源

月光博客
月光博客
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
M
MIT News - Artificial intelligence
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
I
InfoQ
H
Hacker News: Front Page
D
Docker
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
博客园 - 【当耐特】
T
Tor Project blog
U
Unit 42
H
Heimdal Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy & Cybersecurity Law Blog
PCI Perspectives
PCI Perspectives
美团技术团队
O
OpenAI News
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MyScale Blog
MyScale Blog

博客园 - 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-04-13 · via 博客园 - DotNet菜园

3.3、客户端编译生成GRPC类

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

2. 在“解决方案资源管理器”中,使用鼠标左键选中项目名称“Demo.Grpc.Cmd,在弹出的快捷菜单中选择“在文件资源管理器中打开文件夹”菜单项。如下图。

 

3.我们打开“文件资源管理器”,进入到Demo.Grpc.Cmd\obj\Debug\ net7.0目录,发现此时目录下也有与服务端一样的4个.cs文件,就是GRPC协议文件对应的类文件,如下图所示:

3.4、gRPC服务的https调用

1.在服务端项目(Demo.GrpcService)中,由Visual Studio 2022在创建项目时默认配置了两个地址,让我们来调用。2个地址分别为:http://localhost:5209https://localhost:7149, gRPC客户端会使用到这2个地址,目的是给客户端请求请求地址,服务端将监听这两个端口。

2. 在Visual Studio 2022的“解决方案资源管理器”中,使用鼠标右键单击“Demo.Grpc.Cmd”项目名称,在弹出菜单中选择“添加--> 类”。 在“添加新项”对话框中将类命名为 User,然后选择“添加”。

3. 在Visual Studio 2022的“解决方案资源管理器”中,使用鼠标双击打开刚才创建的User.cs文件,添加如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Grpc.Net.Client;
using Demo.GrpcService.Protos;

namespace Demo.Grpc.Cmd
{
 
    public class User

    {

        public void GetUserInfo()
        {

            // 使用https
            const string urlHttps = "https://localhost:7149";

            using (var channel = GrpcChannel.ForAddress(urlHttps))
            {

                var client = new UserInfo.UserInfoClient(channel);

                UserInfoResult userInfo = client.GetUserInfo(new UserInfoRequest()
                {
                    UserName = "Admin",

                    Password = "12345"

                });

                //打印服务方法返回的结果
                Console.WriteLine($"{userInfo.UserName},{userInfo.Age},{userInfo.Name}");
                Console.WriteLine( JsonSerializer.Serialize(userInfo));
            }

           // return string.Empty;
           Console.ReadKey();

        }
    }
}

4. 在Visual Studio 2022的“解决方案资源管理器”中,使用鼠标双击打开program.cs文件,添加如下代码:

/ 、See https://aka.ms/new-console-template for more information

using Demo.Grpc.Cmd;


Console.WriteLine("Hello, World!"); 

new User().GetUserInfo();

5.我们在开启一个Visual Studio 2022,打开“Demo.GrpcService”解决方案,将“Demo.GrpcService”设置为启动项目,并使用https协议启动运行。

 

6.启动运行之后的结果如图。

7.我们切换到“Demo.Grpc.Cmd”为启动项目Visual Studio 2022,按F5,启动。

 

8.启动之后的运行结果,如图。

到此,调用gRPC服务端提供的https地址就成功了。

3.5、gRPC服务的http调用

相比https的调用,我们只需要在调用前加上如下代码即可:

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

1. 在Visual Studio 2022的“解决方案资源管理器”中,使用鼠标双击打开User.cs文件,添加如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Grpc.Net.Client;
using Demo.GrpcService.Protos;

namespace Demo.Grpc.Cmd
{
    public class User
    {

        public void GetUserInfo()
        {
            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            const string urlHttp = "http://localhost:5209";
 
            using (var channel = GrpcChannel.ForAddress(urlHttp))
                // 使用https
               // const string urlHttps = "https://localhost:7149";
           // using (var channel = GrpcChannel.ForAddress(urlHttps))
            {

                var client = new UserInfo.UserInfoClient(channel);

                UserInfoResult userInfo = client.GetUserInfo(new UserInfoRequest()
                {
                    UserName = "Admin",
                    Password = "12345"

                });


                //打印服务方法返回的结果
                Console.WriteLine($"{userInfo.UserName},{userInfo.Age},{userInfo.Name}");
                Console.WriteLine( JsonSerializer.Serialize(userInfo));
            }

           // return string.Empty;
           Console.ReadKey();
        }
    }
}

2.在目Visual Studio 2022,按F5或是点击工具栏上的“运行”按钮,启动“Demo.Grpc.Cmd”控制台程序。

 

到此,调用gRPC服务端提供的http地址就成功了。

运行效果如下: