当前位置: 首页 > news >正文

WPF实战项目十九(客户端):修改RestSharp的引用

 修改HttpRestClient,更新RestSharp到110.2.0,因为106版本和110版本的代码不一样,所以需要修改下代码

 

using Newtonsoft.Json;
using RestSharp;
using System;
using System.Threading.Tasks;
using WPFProjectShared;namespace WPFProject.Service
{public class HttpRestClient{public readonly string apiUrl;//private readonly RestClient client;public HttpRestClient(string apiUrl){this.apiUrl = apiUrl;//client = new RestClient();}public async Task<WebApiResponse> ExecuteAsync(BaseRequest baseRequest){var request = new RestRequest(string.Empty, baseRequest.Method);request.AddHeader("Content-Type", baseRequest.ContenType.ToString());if (baseRequest.Parameter != null)request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);var options = new RestClientOptions();options.BaseUrl = new Uri(apiUrl + baseRequest.Route);var client = new RestClient(options);var response = await client.ExecuteAsync(request);JsonSerializerSettings jsSetting = new JsonSerializerSettings();jsSetting.NullValueHandling = NullValueHandling.Ignore;if (response.StatusCode == System.Net.HttpStatusCode.OK)return JsonConvert.DeserializeObject<WebApiResponse>(response.Content, jsSetting);elsereturn new WebApiResponse(){Status = false,Result = null,Message = response.ErrorMessage};}public async Task<WebApiResponse<T>> ExecuteAsync<T>(BaseRequest baseRequest){var request = new RestRequest(string.Empty, baseRequest.Method);request.AddHeader("Content-Type", baseRequest.ContenType);if (baseRequest.Parameter != null)request.AddParameter("application/json", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);var options = new RestClientOptions();options.BaseUrl = new Uri(apiUrl + baseRequest.Route);var client = new RestClient(options);var response = await client.ExecuteAsync(request);JsonSerializerSettings jsSetting = new JsonSerializerSettings();jsSetting.NullValueHandling = NullValueHandling.Ignore;if (response.StatusCode == System.Net.HttpStatusCode.OK)return JsonConvert.DeserializeObject<WebApiResponse<T>>(response.Content, jsSetting);elsereturn new WebApiResponse<T>(){Status = false,Message = response.ErrorMessage};}}
}

Baseservice.cs也要相应的修改

using DryIoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFProjectAPI.Context.UnitOfWork;
using WPFProjectShared;
using WPFProjectShared.Parameters;namespace WPFProject.Service
{public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class{private readonly HttpRestClient client;private readonly string serviceName;public BaseService(HttpRestClient client, string serviceName){this.client = client;this.serviceName = serviceName;}public async Task<WebApiResponse<TEntity>> AddAsync(TEntity entity){BaseRequest request = new BaseRequest();request.Method = RestSharp.Method.Post;request.Route = $"api/{serviceName}/Add";request.Parameter = entity;return await client.ExecuteAsync<TEntity>(request);}public async Task<WebApiResponse> DeleteAsync(int id){BaseRequest request = new BaseRequest();request.Method = RestSharp.Method.Delete;request.Route = $"api/{serviceName}/Delete?id={id}";return await client.ExecuteAsync(request);}public async Task<WebApiResponse<PagedList<TEntity>>> GetAllPageListAsync(QueryParameter parameter){BaseRequest request = new BaseRequest();request.Method = RestSharp.Method.Get;request.Route = $"api/{serviceName}/GetAllPageList?pageIndex={parameter.PageIndex}" + $"&pageSize={parameter.PageSize}" + $"&search={parameter.Search}";return await client.ExecuteAsync<PagedList<TEntity>>(request);}public async Task<WebApiResponse<TEntity>> GetFirstOfDefaultAsync(int id){BaseRequest request = new BaseRequest();request.Method = RestSharp.Method.Get;request.Route = $"api/{serviceName}/Get?Id={id}";return await client.ExecuteAsync<TEntity>(request);}public async Task<WebApiResponse<TEntity>> UpdateAsync(TEntity entity){BaseRequest request = new BaseRequest();request.Method = RestSharp.Method.Post;request.Route = $"api/{serviceName}/Update";request.Parameter = entity;return await client.ExecuteAsync<TEntity>(request);}}
}

http://www.lryc.cn/news/249977.html

相关文章:

  • kobs-ng 烧写nand中的uboot
  • 【Java】扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问该用户是否要删除该文件
  • PyQt基础_008_ 按钮类控件QSpinbox
  • 3D点云目标检测:VoxelNex解读
  • opencv-利用DeepLabV3+模型进行图像分割去除输入图像的背景
  • 中国版的 GPTs:InsCode AI 生成应用
  • MySQL 学习笔记(刷题篇)
  • windows系统如何配置yarn环境变量
  • 视频中的文字水印怎么去除?这三招学会轻松去视频水印
  • Java项目学生管理系统二查询所有
  • 27.Spring如何避免在并发下获取不完整的Bean?
  • 浅析SD-WAN企业组网部署中简化网络运维的关键技术
  • 【Rust】快速教程——自定义类型、数字转枚举、Cargo运行
  • python 实现 AIGC 大语言模型中的概率论:生日相同问题的代码场景模拟
  • SD-WAN组网中的CPE及云服务CPE部署方法
  • 理解BatchNormalization层的作用
  • uniapp实现文件预览过程
  • 深度学习-学习笔记记录
  • 程序员养生之道:延寿不忘初心——延寿必备
  • 使用Docker安装部署Swagger Editor并远程访问编辑API文档
  • Nacos 2.X核心架构源码剖析
  • C语言--每日选择题--Day31
  • chrome vue devTools安装
  • Spring Security 6.x 系列(7)—— 源码分析之Builder设计模式
  • PyQt6 中自定义浮点型滑块类
  • 笔记,B+树
  • 代码随想录刷题题Day2
  • 【JAVA面向对象编程】--- 探索子类如何继承父类
  • 从浏览器控制台发送get,post请求
  • 海外问卷调查怎么批量做?可以用指纹浏览器吗?