7 changed files with 126 additions and 195 deletions
@ -1,90 +0,0 @@ |
|||||
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.DbFinder.EventBus.Distributed |
|
||||
{ |
|
||||
public class ConnectionStringChangedEventHandler : |
|
||||
IDistributedEventHandler<ConnectionStringCreatedEventData>, |
|
||||
IDistributedEventHandler<ConnectionStringDeletedEventData>, |
|
||||
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(ConnectionStringCreatedEventData 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); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
using (_currentTenant.Change(null)) |
|
||||
{ |
|
||||
await _cache.RemoveManyAsync( |
|
||||
new string[] |
|
||||
{ |
|
||||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), |
|
||||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
_logger.LogException(ex); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,67 +0,0 @@ |
|||||
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; |
|
||||
using Volo.Abp.MultiTenancy; |
|
||||
|
|
||||
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed |
|
||||
{ |
|
||||
public class ConnectionStringChangedEventHandler : |
|
||||
IDistributedEventHandler<ConnectionStringCreatedEventData>, |
|
||||
IDistributedEventHandler<ConnectionStringDeletedEventData>, |
|
||||
ITransientDependency |
|
||||
{ |
|
||||
private readonly ICurrentTenant _currentTenant; |
|
||||
private readonly ITenantAppService _tenantAppService; |
|
||||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|
||||
|
|
||||
public ConnectionStringChangedEventHandler( |
|
||||
ICurrentTenant currentTenant, |
|
||||
ITenantAppService tenantAppService, |
|
||||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|
||||
{ |
|
||||
_cache = cache; |
|
||||
_currentTenant = currentTenant; |
|
||||
_tenantAppService = tenantAppService; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData) |
|
||||
{ |
|
||||
using (_currentTenant.Change(null)) |
|
||||
{ |
|
||||
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); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) |
|
||||
{ |
|
||||
using (_currentTenant.Change(null)) |
|
||||
{ |
|
||||
await _cache.RemoveManyAsync( |
|
||||
new string[] |
|
||||
{ |
|
||||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), |
|
||||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue