.netcore grpc客户端工厂及依赖注入使用

一、客户端工厂概述

  1. gRPC 与 HttpClientFactory 的集成提供了一种创建 gRPC 客户端的集中方式。
  2. 可以通过依赖包Grpc.Net.ClientFactory中的AddGrpcClient进行gRPC客户端依赖注入
  3. AddGrpcClient函数提供了许多配置项用于处理一些其他事项;例如AOP、重试策略等

二、案例介绍

  1. 创建一个WPF客户端
  2. 在App.xaml.cs代码类里重写OnStartup方法,进行依赖注入;需要注意的是在方法内设立窗口调用需要把展示端属性  xmlns: StartupUri="FieldWindow.xaml 给去掉
  3. 引用ServiceCollection做为容器集
  4. 注入gRPC工厂,注入windows窗体,以及其他需要用到的服务类等

三、客户端代码展示

  1. proto文件 可以看我之前的文章这里就不放上来了
    /// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : Application{protected override void OnStartup(StartupEventArgs e){IServiceCollection services = new ServiceCollection();// 注入services.AddWPFGrpc();AddWPFWindows(services);// 构建服务提供器var serviceProvider = services.BuildServiceProvider();var fieldWindow = serviceProvider.GetRequiredService<FieldWindow>();fieldWindow.Show();}private IServiceCollection AddWPFWindows(IServiceCollection services){if (services == null){throw new ArgumentNullException(nameof(services));}var windowType = typeof(Window);var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.BaseType == windowType).ToList();foreach (var type in types){services.AddScoped(type);}return services;}}
    public static class GrpcClient{/// <summary>/// rpc 工厂注入/// </summary>/// <param name="services"></param>/// <returns></returns>public static IServiceCollection AddWPFGrpc(this IServiceCollection services){if (services == null){throw new ArgumentNullException(nameof(services));}services.AddGrpcClient<FieldRpc.FieldRpcClient>(options => options.Address = new Uri("https://localhost:7188"));return services;}}
    /// <summary>/// FieldWindow.xaml 的交互逻辑/// </summary>public partial class FieldWindow : Window{private readonly FieldRpc.FieldRpcClient _fieldRpcClient;public FieldWindow( FieldRpc.FieldRpcClient fieldRpcClient){InitializeComponent();_fieldRpcClient = fieldRpcClient;}// 基础private async void BtnBaseconfig_Click(object sender, RoutedEventArgs e){// 基础BaseConfig config = new BaseConfig();config.Name = "张三";config.Position = 2.33D;config.Distance = 5.48F;config.Age = 10;config.TimeSpanId = 6538590027736100;config.SAge = 1921;config.STimeSpanId = 6538590027736130;config.Flag = true;await _fieldRpcClient.BaseConfigServiceAsync(config);MessageBox();}// 日期private async void BtnDateconfig_Click(object sender, RoutedEventArgs e){// 日期DateConfig dateConfig = new DateConfig();dateConfig.Id = 179;dateConfig.DateDuration = Google.Protobuf.WellKnownTypes.Duration.FromTimeSpan(TimeSpan.FromSeconds(5));// 注意这里的时间是utc时间dateConfig.DateTimestamp = Timestamp.FromDateTime(DateTime.UtcNow);await _fieldRpcClient.DateConfigServiceAsync(dateConfig);MessageBox();}// 字节private async void BtnByteconfig_Click(object sender, RoutedEventArgs e){// 字节ByteConfig byteConfig = new ByteConfig();byteConfig.Id = 9854564654654;byteConfig.PositionBytes = ByteString.CopyFrom(Encoding.UTF8.GetBytes("庄这人的南的很"));await _fieldRpcClient.ByteConfigServiceAsync(byteConfig);MessageBox();}// nullprivate async void BtnNullconfig_Click(object sender, RoutedEventArgs e){// nullNullConfig nullConfig = new NullConfig();nullConfig.Id = 1854564654654;nullConfig.NullBool = true;nullConfig.NullFloat = null;nullConfig.NullUInt = null;nullConfig.NullInt = 15;nullConfig.NullLong = 112345451234787;await _fieldRpcClient.NullConfigServiceAsync(nullConfig);MessageBox();}// listprivate async void BtnListconfig_Click(object sender, RoutedEventArgs e){// ListConfigListConfig listConfig = new ListConfig();var attributes = new Dictionary<int, string>{[1] = "one",[2] = "two",[3] = "three",[4] = "four",[5] = "five"};listConfig.Id = 123456456;listConfig.Attributes.Add(attributes);var dicDetail = new Dictionary<int, ListDetailConfig>{[1] = new ListDetailConfig { Height = 1, Name = "one" },[2] = new ListDetailConfig { Height = 2, Name = "two" },[3] = new ListDetailConfig { Height = 3, Name = "three" },[4] = new ListDetailConfig { Height = 4, Name = "four" },[5] = new ListDetailConfig { Height = 5, Name = "five" }};listConfig.DicDetail.Add(dicDetail);listConfig.Details.Add(new ListDetailConfig { Height = 8, Name = "Eight" });var detailConfigs = new List<ListDetailConfig>{new ListDetailConfig { Height=9,Name="nine"},new ListDetailConfig{ Height=10,Name="ten"}};listConfig.Details.Add(detailConfigs);await _fieldRpcClient.ListConfigServiceAsync(listConfig);MessageBox();}// anyprivate async void BtnAnyconfig_Click(object sender, RoutedEventArgs e){// AnyAnyConfig anyConfig = new AnyConfig();anyConfig.Id = 42564134;anyConfig.AnyObject = Any.Pack(new B { Id = 15 });await _fieldRpcClient.AnyConfigServiceAsync(anyConfig);MessageBox();}// Oneofprivate async void BtnOneofconfig_Click(object sender, RoutedEventArgs e){// OneofOneofConfig oneofConfig = new OneofConfig();oneofConfig.OA = new A { Id = 1 };//oneofConfig.OC = new C { Id = 2 };await _fieldRpcClient.OneofConfigServiceAsync(oneofConfig);MessageBox();}private void MessageBox(){string messageBoxText = "执行完成";string caption = "HELLO";MessageBoxButton button = MessageBoxButton.OK;MessageBoxImage icon = MessageBoxImage.Information;MessageBoxResult result = System.Windows.MessageBox.Show(messageBoxText, caption, button, icon);}}

四、执行效果展示

客户端:

 服务端:

 五、源码地址

链接:https://pan.baidu.com/s/1FQY7QOgF8Y90igKV56Yupg 
提取码:mbyg

下一篇https://blog.csdn.net/qq_31975127/article/details/132346657

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/76010.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微服务中间件--分布式事务

分布式事务 a.理论基础1) CAP定理2) BASE理论 b.Seata1) XA模式1.a) 实现XA模式 2) AT模式3) TCC模式3.a) 代码实现 4) Saga模式5) 四种模式对比6) TC的异地多机房容灾架构 a.理论基础 1) CAP定理 分布式系统有三个指标&#xff1a; Consistency&#xff08;一致性&#xff…

【仿写tomcat】四、解析http请求信息,响应给前端,HttpServletRequest、HttpServletResponse的简单实现

思考 在解析请求之前我们要思考一个问题&#xff0c;我们解析的是其中的哪些内容&#xff1f; 对于最基本的实现&#xff0c;当然是请求类型&#xff0c;请求的url以及请求参数&#xff0c;我们可以根据请求的类型作出对应的处理&#xff0c;通过url在我们的mapstore中找到se…

java+springboot+mysql银行管理系统

项目介绍&#xff1a; 使用javaspringbootmysql开发的银行管理系统&#xff0c;系统包含超级管理员、管理员、客户角色&#xff0c;功能如下&#xff1a; 超级管理员&#xff1a;管理员管理&#xff1b;客户管理&#xff1b;卡号管理&#xff08;存款、取款、转账&#xff09…

线性代数的学习和整理7:各种特殊矩阵(草稿-----未完成)

目录 1 单位矩阵 为什么单位矩阵I是 [1,0;0,1]T 而不是[1,1;1,1]T 2 旋转矩阵 3 伸缩矩阵 放大缩小倍数矩阵 4 镜像矩阵 5 剪切矩阵 矩阵 行向量 列向量 方阵 1 单位矩阵 [ 1 0 0 1] 为什么单位矩阵I是 [1,0;0,1]T 而不是[1,1;1,1]T 因为 矩阵 [1,0;0,1] 代表…

《TCP IP网络编程》第二十四章

第 24 章 制作 HTTP 服务器端 24.1 HTTP 概要 本章将编写 HTTP&#xff08;HyperText Transfer Protocol&#xff0c;超文本传输协议&#xff09;服务器端&#xff0c;即 Web 服务器端。 理解 Web 服务器端&#xff1a; web服务器端就是要基于 HTTP 协议&#xff0c;将网页对…

CVE-2023-21292 AMS框架层高危漏洞分析

文章目录 前言漏洞细节故事起源漏洞利用漏洞修复 总结 前言 本周在分析 Google 官方发布的 Android 2023 年8 月安全公告 涉及的漏洞补丁的时候&#xff0c;遇到一个有意思的漏洞&#xff1a;CVE-2023-21292。 之所以说它有意思是因为这个漏洞早在去年年底就在某平台上被国外…

Git分布式版本控制系统

目录 2、安装git 2.1 初始环境 2.2 Yum安装Git 2.3 编译安装 2.4 初次运行 Git 前的配置 2.5 初始化及获取 Git 仓库 2.6 Git命令常规操作 2.6.2 添加新文件 2.6.3 删除git内的文件 2.6.4 重命名暂存区数据 2.6.5 查看历史记录 2.6.6 还原历史数据 2.6.7 还原未来…

泰迪大数据挖掘建模平台功能特色介绍

大数据挖掘建模平台面相高校、企业级别用户快速进行数据处理的建模工具。 大数据挖掘建模平台介绍 平台底层算法基于R语言、Python、Spark等引擎&#xff0c;使用JAVA语言开发&#xff0c;采用 B/S 结构&#xff0c;用户无需下载客户端&#xff0c;可直接通过浏览器进行…

【JavaSE】面向对象之多态

文章目录 多态的概念多态实现条件重写向上转型和向下转型向上转型向下转型 避免在构造方法中调用重写的方法 多态的概念 通俗来说&#xff0c;就是多种形态&#xff0c;具体点就是去完成某个行为&#xff0c;当不同的对象去完成时会产生出不同的状态。 多态实现条件 必须要…

Android JNI系列详解之JNI、NDK环境搭建和编译工具安装

本文主要介绍JNI、NDK环境变量的搭建&#xff0c;以及CMake工具的安装和ndk-build工具的安装。 一、JNI环境 JNI属于Java中的一部分&#xff0c;所以只需要搭建Java环境就有了JNI的环境&#xff0c;安装Java的环境可以网上查找教程&#xff0c;很多的安装JDK的博客。我电脑是安…

16.5.4 【Linux】SELinux 政策内的规则管理

SELinux 各个规则的布林值查询 getsebool 如果想要查询系统上面全部规则的启动与否 &#xff08;on/off&#xff0c;亦即布林值&#xff09;&#xff0c;很简单的通过 sestatus-b 或 getsebool -a 均可&#xff01; SELinux 各个规则规范的主体程序能够读取的文件 SELinux typ…

点亮一颗LED灯

TOC LED0 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能APB2的外设时钟GPIO_InitTypeDef GPIO_Initstructure;GPIO_Initstructure.GPIO_Mode GPIO_Mode_Out_PP;//通用推挽输出GPIO_Initstructure.GPIO_Pin GPIO_Pin_5;GPIO_Initstructure.GPIO_Speed GPIO_S…