修改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);
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
};
}
}
}
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);
}
}
}
原文地址:https://blog.csdn.net/yigu4011/article/details/134715290
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_39622.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。