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

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

cs 复制代码
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);
            else
                return 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);

            else
                return new WebApiResponse<T>()
                {
                    Status = false,
                    Message = response.ErrorMessage
                };
        }
    }
}

Baseservice.cs也要相应的修改

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);
        }
    }
}
相关推荐
林子漾13 小时前
【paper】分布式无人水下航行器围捕智能目标
分布式·wpf
wyh要好好学习19 小时前
C# WPF 记录DataGrid的表头顺序,下次打开界面时应用到表格中
开发语言·c#·wpf
咩咩觉主20 小时前
尽量通俗易懂地概述.Net && U nity跨语言/跨平台相关知识
unity·c#·.net·.netcore
lgcgkCQ1 天前
任务调度中心-XXL-JOB使用详解
java·wpf·定时任务·任务调度
Vicky&James1 天前
英雄联盟客户端项目:从跨平台Uno Platform到Win UI3的转换只需要30分钟
github·wpf·跨平台·英雄联盟·winui·unoplatform
陈逸子风2 天前
(系列十一)Vue3框架中路由守卫及请求拦截(实现前后端交互)
vue3·webapi·权限·流程·表单
就是有点傻2 天前
WPF中如何使用区域导航
wpf
她说彩礼65万2 天前
WPF程序设置单例启动(互斥体)
wpf
就是有点傻2 天前
WPF中Prism框架中 IContainerExtension 和 IRegionManager的作用
wpf
月落.2 天前
WPF中MVVM工具包 CommunityToolkit.Mvvm
wpf·mvvm