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

推荐订阅源

S
Schneier on Security
F
Fortinet All Blogs
博客园_首页
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
D
Docker
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
量子位
The Last Watchdog
The Last Watchdog
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
Cloudbric
Cloudbric
博客园 - 司徒正美
H
Help Net Security
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LangChain Blog
Latest news
Latest news
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
博客园 - Franky
S
Security Affairs
W
WeLiveSecurity
F
Full Disclosure
Know Your Adversary
Know Your Adversary
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
美团技术团队
PCI Perspectives
PCI Perspectives
C
Check Point Blog
Spread Privacy
Spread Privacy

博客园 - 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-06-08 · via 博客园 - DotNet菜园

3.12、依赖注入方式调用gRPC

1. 在Visual Studio 2022的解决方案资源管理器中,使用鼠标右键单击“Command”文件夹,在弹出菜单中选择“添加--> 类”,在弹出的“添加新项”对话框中,选择添加 “UserIoc.cs”类,这是一个我们要实现的依赖注入的类,然后选择“添加”。

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

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

namespace Demo.Grpc.Client
{
    internal class UserIoc
    {

        /// <summary>
        /// 定义gRPC客户端服务对象
        /// </summary>

        private readonly UserInfo.UserInfoClient _userClient;

        public UserIoc(UserInfo.UserInfoClient userClient)

        {

            _userClient = userClient;
        }

        public string GetUserInfo()
        {
            var userInfo = _userClient.GetUserInfo(new UserInfoRequest()
            {

                UserName="IocTest",
                Password = "GRPC 依赖注入调用方式- IOC"

            });

            return JsonSerializer.Serialize(userInfo);

        }
}

}

 

3. 在MainWindows.xmal文件中添加一个Buttion控件,并使用鼠标双击这个按钮,在MainWindows.xmal.cs文件中添加一个btnIocTestUserInfo_Click事件,具体代码如下:

<Button x:Name="btnIocTestUserInfo" Grid.Column="2" Grid.Row="0" Content="Ioc用户信息" Click="btnIocTestUserInfo_Click"></Button>

4.在MainWindows.xmal.cs文件的btnIocTestUserInfo_Click事件中,添加依赖注入的代码。具体代码如下:

        private void btnIocTestUserInfo_Click(object sender, RoutedEventArgs e)
        {
            #region 使用IOC注入的方式调用gRPC
            IServiceCollection services = new ServiceCollection();

 
            //注册UserIoc服务
            services.AddTransient<UserIoc>();

            #region gRPC Client注册

            //调用http时启用该设置
            //AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            //添加gRPC客户端服务
            services.AddGrpcClient<UserInfo.UserInfoClient>(options =>
            {
                //设置gRPC的https服务调用地址
                options.Address = new Uri("https://localhost:7149");

            }).ConfigureChannel(grpcOptions =>
            {
            });
            #endregion
            //构建容器
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            //解析UserIoc服务

            var grpcRequestTest = serviceProvider.GetService<UserIoc>();
            //调用UserIoc服务中的GetUserInfo方法

           txtMsg.Text= grpcRequestTest.GetUserInfo();
            #endregion
        }

5.新开一个Visual Studio 2022,打开Demo.GrpcService解决方案,并在Visual Studio 2022的解决方案资源管理器中,将Demo.GrpcService项目设为启动项目。按F5,启动。如图。

 

6.在第一个Visual Studio 2022中,我们按F5,将Grpc.Demo.Client运行起来。然后点击“Ioc用户信息”按钮,实现Ioc调用grpc的方法 。如图