本文主要是介绍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);}}
}
这篇关于WPF实战项目十九(客户端):修改RestSharp的引用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!