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

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.Http.Json;
using System.Net.Security; using System.Net.Security;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; 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 try
{ {
HttpClient httpClient = GetHttpClient(); 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 try
{ {
HttpClient httpClient = GetHttpClient(); HttpClient httpClient = GetHttpClient();
@@ -117,7 +117,8 @@ public class HttpClientHelper : IHttpHelper
} }
} }
public bool DownloadFile(string url, string localPath)
public bool DownloadFile(string url, string localPath)
{ {
ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true; ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true;
HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest; HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Hua.DotNet.Code.Helper.Http; namespace Hua.DotNet.Code.Helper.Http;
@@ -10,9 +11,9 @@ public interface IHttpHelper
Dictionary<string, string> Headers { get; set; } 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); bool DownloadFile(string url, string fileFullName);
} }

View File

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