4 changed files with 125 additions and 7 deletions
@ -0,0 +1,68 @@ |
|||||
|
using LINGYUN.Abp.MultiTenancy.DbFinder; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Caching; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.EventBus.Distributed; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.TenantManagement; |
||||
|
using Volo.Abp.Uow; |
||||
|
|
||||
|
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed |
||||
|
{ |
||||
|
public class ConnectionStringChangedEventHandler : IDistributedEventHandler<ConnectionStringChangedEventData>, ITransientDependency |
||||
|
{ |
||||
|
private readonly ILogger<ConnectionStringChangedEventHandler> _logger; |
||||
|
private readonly ICurrentTenant _currentTenant; |
||||
|
private readonly ITenantRepository _tenantRepository; |
||||
|
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
||||
|
|
||||
|
public ConnectionStringChangedEventHandler( |
||||
|
ICurrentTenant currentTenant, |
||||
|
ITenantRepository tenantRepository, |
||||
|
ILogger<ConnectionStringChangedEventHandler> logger, |
||||
|
IDistributedCache<TenantConfigurationCacheItem> cache) |
||||
|
{ |
||||
|
_cache = cache; |
||||
|
_logger = logger; |
||||
|
_currentTenant = currentTenant; |
||||
|
_tenantRepository = tenantRepository; |
||||
|
} |
||||
|
|
||||
|
[UnitOfWork] |
||||
|
public virtual async Task HandleEventAsync(ConnectionStringChangedEventData eventData) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
using (_currentTenant.Change(null)) |
||||
|
{ |
||||
|
var tenant = await _tenantRepository.FindAsync(eventData.Id, true); |
||||
|
if (tenant == null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
var connectionStrings = new ConnectionStrings(); |
||||
|
foreach (var tenantConnectionString in tenant.ConnectionStrings) |
||||
|
{ |
||||
|
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
||||
|
} |
||||
|
var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); |
||||
|
|
||||
|
await _cache.SetAsync( |
||||
|
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), |
||||
|
cacheItem); |
||||
|
|
||||
|
await _cache.SetAsync( |
||||
|
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name), |
||||
|
cacheItem); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogException(ex); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using LINGYUN.Abp.TenantManagement; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Caching; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.EventBus.Distributed; |
||||
|
|
||||
|
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed |
||||
|
{ |
||||
|
public class ConnectionStringChangedEventHandler : IDistributedEventHandler<ConnectionStringChangedEventData>, ITransientDependency |
||||
|
{ |
||||
|
private readonly ITenantAppService _tenantAppService; |
||||
|
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
||||
|
|
||||
|
public ConnectionStringChangedEventHandler( |
||||
|
ITenantAppService tenantAppService, |
||||
|
IDistributedCache<TenantConfigurationCacheItem> cache) |
||||
|
{ |
||||
|
_cache = cache; |
||||
|
_tenantAppService = tenantAppService; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public virtual async Task HandleEventAsync(ConnectionStringChangedEventData eventData) |
||||
|
{ |
||||
|
var tenantDto = await _tenantAppService.GetAsync(eventData.Id); |
||||
|
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Id); |
||||
|
var connectionStrings = new ConnectionStrings(); |
||||
|
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) |
||||
|
{ |
||||
|
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
||||
|
} |
||||
|
var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); |
||||
|
|
||||
|
await _cache.SetAsync( |
||||
|
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), |
||||
|
cacheItem); |
||||
|
|
||||
|
await _cache.SetAsync( |
||||
|
TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), |
||||
|
cacheItem); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace LINGYUN.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class ConnectionStringChangedEventData |
||||
|
{ |
||||
|
public Guid Id { get; set; } |
||||
|
|
||||
|
public string OriginName { get; set; } |
||||
|
|
||||
|
public string Name { get; set; } |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue