|
@@ -5,50 +5,56 @@ using System.Net.Http.Headers;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using IdentityModel.Client;
|
|
|
+using Microsoft.Extensions.DependencyInjection;
|
|
|
using Sharing.Province.Dtos;
|
|
|
using Sharing.Province.Extensions;
|
|
|
+using SqlSugar;
|
|
|
using XF.Domain.Cache;
|
|
|
using XF.Domain.Dependency;
|
|
|
using XF.Domain.Exceptions;
|
|
|
|
|
|
namespace Sharing.Province
|
|
|
{
|
|
|
- public class ProvinceClient : IScopeDependency, ISelfDependency
|
|
|
+ public class ProvinceClient : ISingletonDependency, ISelfDependency
|
|
|
{
|
|
|
private static readonly string KeyToken = "KeyToken";
|
|
|
- private readonly IChannelConfigurationManager _channelConfigurationManager;
|
|
|
- private readonly IHttpInvoker _httpInvoker;
|
|
|
- private readonly ITypedCache<TokenInfo> _cacheToken;
|
|
|
+ private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
|
- public ProvinceClient(
|
|
|
- IChannelConfigurationManager channelConfigurationManager,
|
|
|
- IHttpInvoker httpInvoker,
|
|
|
- ITypedCache<TokenInfo> cacheToken)
|
|
|
+ public ProvinceClient(IServiceScopeFactory scopeFactory)
|
|
|
{
|
|
|
- _channelConfigurationManager = channelConfigurationManager;
|
|
|
- _httpInvoker = httpInvoker;
|
|
|
- _cacheToken = cacheToken;
|
|
|
+ _scopeFactory = scopeFactory;
|
|
|
}
|
|
|
|
|
|
public async Task<TResponse?> RequestAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
|
|
|
where TRequest : IProvinceRequest, new()
|
|
|
{
|
|
|
- var token = _cacheToken.GetOrSet(KeyToken,
|
|
|
+ using var scope = _scopeFactory.CreateScope();
|
|
|
+ var provider = scope.ServiceProvider;
|
|
|
+ var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();
|
|
|
+ var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
|
|
|
+ var cacheToken = provider.GetRequiredService<ITypedCache<TokenInfo>>();
|
|
|
+
|
|
|
+ var token = cacheToken.GetOrSet(KeyToken,
|
|
|
d => GetTokenAsync(cancellationToken).GetAwaiter().GetResult(),
|
|
|
TimeSpan.FromMinutes(28));
|
|
|
- var configProvince = _channelConfigurationManager.GetConfigurationProvince();
|
|
|
+ var configProvince = channelconfigManager.GetConfigurationProvince();
|
|
|
request.BuildClientInfo(configProvince.ClientId, configProvince.ClientSecret);
|
|
|
|
|
|
//todo token失效重新获取token再次请求,返回参数缺少该状态,暂无法处理
|
|
|
|
|
|
- return await _httpInvoker.RequestAsync<TRequest, TResponse>(request,
|
|
|
+ return await httpInvoker.RequestAsync<TRequest, TResponse>(request,
|
|
|
d => d.SetHttpClient(configProvince.Address, token?.AccessToken ?? string.Empty),
|
|
|
cancellationToken);
|
|
|
}
|
|
|
|
|
|
private async Task<TokenInfo> GetTokenAsync(CancellationToken cancellationToken)
|
|
|
{
|
|
|
- var configProvince = _channelConfigurationManager.GetConfigurationProvince();
|
|
|
+ using var scope = _scopeFactory.CreateScope();
|
|
|
+ var provider = scope.ServiceProvider;
|
|
|
+ var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();
|
|
|
+ var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
|
|
|
+
|
|
|
+ var configProvince = channelconfigManager.GetConfigurationProvince();
|
|
|
var baseAddress = configProvince.Address;
|
|
|
if (!baseAddress.EndsWith('/'))
|
|
|
baseAddress += "/";
|
|
@@ -66,7 +72,7 @@ namespace Sharing.Province
|
|
|
}
|
|
|
|
|
|
};
|
|
|
- var tokenResponse = await _httpInvoker.GetTokenAsync(request, cancellationToken);
|
|
|
+ var tokenResponse = await httpInvoker.GetTokenAsync(request, cancellationToken);
|
|
|
if (tokenResponse is null)
|
|
|
throw new UserFriendlyException("获取token请求失败");
|
|
|
if (tokenResponse.IsError)
|