修改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 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(response.Content, jsSetting);
else
return new WebApiResponse()
{
Status = false,
Result = null,
Message = response.ErrorMessage
};
}
public async Task> ExecuteAsync(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>(response.Content, jsSetting);
else
return new WebApiResponse()
{
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 : IBaseService 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> AddAsync(TEntity entity)
{
BaseRequest request = new BaseRequest();
request.Method = RestSharp.Method.Post;
request.Route = $"api/{serviceName}/Add";
request.Parameter = entity;
return await client.ExecuteAsync(request);
}
public async Task 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>> 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>(request);
}
public async Task> GetFirstOfDefaultAsync(int id)
{
BaseRequest request = new BaseRequest();
request.Method = RestSharp.Method.Get;
request.Route = $"api/{serviceName}/Get?Id={id}";
return await client.ExecuteAsync(request);
}
public async Task> UpdateAsync(TEntity entity)
{
BaseRequest request = new BaseRequest();
request.Method = RestSharp.Method.Post;
request.Route = $"api/{serviceName}/Update";
request.Parameter = entity;
return await client.ExecuteAsync(request);
}
}
}