public virtual async Task HandleEventAsync(EntityCreatedEto <TenantEto> eventData)
        {
            try
            {
                using (_currentTenant.Change(null))
                {
                    var tenant = await _tenantRepository.FindAsync(eventData.Entity.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);

                    var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString());
                    await _cache.SetAsync(cacheKey, cacheItem);
                }
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
        }
コード例 #2
0
        public async Task DeleteAsync(Guid id)
        {
            var tenant = await _tenantRepository.FindAsync(id);

            if (tenant == null)
            {
                return;
            }

            await _tenantRepository.DeleteAsync(tenant);
        }
コード例 #3
0
ファイル: TenantStore.cs プロジェクト: zxbe/abp
        public async Task <TenantConfiguration> FindAsync(Guid id)
        {
            using (_currentTenant.Change(null)) //TODO: No need this if we can implement to define host side (or tenant-independent) entities!
            {
                var tenant = await _tenantRepository.FindAsync(id).ConfigureAwait(false);

                if (tenant == null)
                {
                    return(null);
                }

                return(_objectMapper.Map <Tenant, TenantConfiguration>(tenant));
            }
        }
コード例 #4
0
        protected virtual async Task <TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id)
        {
            using (_currentTenant.Change(null))
            {
                var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString());

                Logger.LogDebug($"TenantStore.GetCacheItemByIdAsync: {cacheKey}");

                var cacheItem = await _cache.GetAsync(cacheKey);

                if (cacheItem != null)
                {
                    Logger.LogDebug($"Found in the cache: {cacheKey}");
                    return(cacheItem);
                }
                Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}");

                var tenant = await _tenantRepository.FindAsync(id, true);

                if (tenant == null)
                {
                    Logger.LogWarning($"Can not found tenant by id: {id}");
                    // throw new AbpException($"Can not found tenant by id: {id}");
                    return(null);
                }
                var connectionStrings = new ConnectionStrings();
                foreach (var tenantConnectionString in tenant.ConnectionStrings)
                {
                    connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
                }
                cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings);

                Logger.LogDebug($"Setting the cache item: {cacheKey}");
                await _cache.SetAsync(cacheKey, cacheItem);

                // 用租户名称再次缓存,以便通过标识查询也能命中缓存
                await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name), cacheItem);

                Logger.LogDebug($"Finished setting the cache item: {cacheKey}");

                return(cacheItem);
            }
        }
        public virtual async Task HandleEventAsync(EntityUpdatedEto <TenantEto> eventData)
        {
            // 禁用租户过滤器
            using (_dataFilter.Disable <IMultiTenant>())
            {
                var tenant = await _tenantRepository.FindAsync(eventData.Entity.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);

                var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString());
                await _cache.SetAsync(cacheKey, cacheItem);
            }
        }