解决异步接口不能取消问题

This commit is contained in:
2025-06-24 13:56:59 +08:00
parent f4acbffd32
commit b110ff52bc
3 changed files with 23 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ using System.Net.Http;
using System.Net.Http.Json;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
@@ -68,9 +69,9 @@ public class HttpClientHelper : IHttpHelper
}
}
public async Task<TOut?> PostAsync<TIn, TOut>(string url, TIn input, int timeOut = 3) where TIn : class where TOut : class
public async Task<TOut?> PostAsync<TIn, TOut>(string url, TIn input, int timeOut = 3, CancellationToken? cts = null) where TIn : class where TOut : class
{
_ = 2;
try
{
HttpClient httpClient = GetHttpClient();
@@ -93,9 +94,8 @@ public class HttpClientHelper : IHttpHelper
}
}
public async Task<TOut?> GetAsync<TOut>(string url, int timeOut = 10)
public async Task<TOut?> GetAsync<TOut>(string url, int timeOut = 10,CancellationToken? cts = null)
{
_ = 2;
try
{
HttpClient httpClient = GetHttpClient();
@@ -117,6 +117,7 @@ public class HttpClientHelper : IHttpHelper
}
}
public bool DownloadFile(string url, string localPath)
{
ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Hua.DotNet.Code.Helper.Http;
@@ -10,9 +11,9 @@ public interface IHttpHelper
Dictionary<string, string> Headers { get; set; }
Task<TOut?> PostAsync<TIn, TOut>(string url, TIn input, int timeOut = 10) where TIn : class where TOut : class;
Task<TOut?> PostAsync<TIn, TOut>(string url, TIn input, int timeOut = 10,CancellationToken? cts = null) where TIn : class where TOut : class;
Task<TOut?> GetAsync<TOut>(string url, int timeOut = 10);
Task<TOut?> GetAsync<TOut>(string url, int timeOut = 10, CancellationToken? cts = null);
bool DownloadFile(string url, string fileFullName);
}

View File

@@ -21,32 +21,29 @@ public class RestClientHttpHelper : IHttpHelper
_logger = logger;
}
public virtual RestClient GetHttpClient()
protected virtual RestClient GetHttpClient()
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_000f: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Expected O, but got Unknown
RestClient val = ((Handler == null) ? new RestClient() : new RestClient((HttpMessageHandler)Handler, true));
var val = ((Handler == null) ? new RestClient() : new RestClient((HttpMessageHandler)Handler, true));
if (Headers == null)
{
return val;
}
foreach (KeyValuePair<string, string> header in Headers)
{
RestClientExtensions.AddDefaultHeader(val, header.Key, header.Value);
val.AddDefaultHeader(header.Key, header.Value);
}
return val;
}
public async Task<TOut> PostAsync<TIn, TOut>(string url, TIn input, int timeOut = 10) where TIn : class where TOut : class
public async Task<TOut> PostAsync<TIn, TOut>(string url, TIn input, int timeOut = 10, CancellationToken? cts = null) where TIn : class where TOut : class
{
try
{
RestClient client = GetHttpClient();
RestRequest request = RestRequestExtensions.AddJsonBody<TIn>(new RestRequest(url, (Method)0), input, "application/json");
var client = GetHttpClient();
var request = new RestRequest(url, Method.Post).AddJsonBody<TIn>(input, "application/json");
request.Timeout = TimeSpan.FromSeconds(timeOut);
await RestClientExtensions.PostAsync(client, request, CancellationToken.None);
return await RestClientExtensions.PostAsync<TOut>(client, request, default(CancellationToken));
await client.PostAsync(request, cts ?? CancellationToken.None);
return await client.PostAsync<TOut>(request, cts??CancellationToken.None);
}
catch (Exception ex)
{
@@ -56,13 +53,13 @@ public class RestClientHttpHelper : IHttpHelper
}
}
public async Task<TOut?> GetAsync<TOut>(string url, int timeOut = 10)
public async Task<TOut?> GetAsync<TOut>(string url, int timeOut = 10, CancellationToken? cts = null)
{
try
{
RestClient httpClient = GetHttpClient();
RestRequest val = new RestRequest(url, (Method)0);
return await RestClientExtensions.GetAsync<TOut>(httpClient, val, default(CancellationToken));
using RestClient httpClient = GetHttpClient();
var val = new RestRequest(url,Method.Get);
return await httpClient.GetAsync<TOut>(val, cts ?? CancellationToken.None);
}
catch (Exception ex)
{
@@ -72,6 +69,7 @@ public class RestClientHttpHelper : IHttpHelper
}
}
public bool DownloadFile(string url, string fileFullName)
{
throw new NotImplementedException();