|
|
|
|
@ -26,105 +26,146 @@ namespace Volo.Abp.MultiTenancy
|
|
|
|
|
|
|
|
|
|
public override async Task<string> ResolveAsync(string connectionStringName = null)
|
|
|
|
|
{
|
|
|
|
|
//No current tenant, fallback to default logic
|
|
|
|
|
if (_currentTenant.Id == null)
|
|
|
|
|
{
|
|
|
|
|
//No current tenant, fallback to default logic
|
|
|
|
|
return await base.ResolveAsync(connectionStringName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var serviceScope = _serviceProvider.CreateScope())
|
|
|
|
|
var tenant = await FindTenantConfigurationAsync(_currentTenant.Id.Value);
|
|
|
|
|
|
|
|
|
|
if (tenant == null || tenant.ConnectionStrings.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
var tenantStore = serviceScope
|
|
|
|
|
.ServiceProvider
|
|
|
|
|
.GetRequiredService<ITenantStore>();
|
|
|
|
|
//Tenant has not defined any connection string, fallback to default logic
|
|
|
|
|
return await base.ResolveAsync(connectionStringName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tenantDefaultConnectionString = tenant.ConnectionStrings.Default;
|
|
|
|
|
|
|
|
|
|
//Requesting default connection string...
|
|
|
|
|
if (connectionStringName == null ||
|
|
|
|
|
connectionStringName == ConnectionStrings.DefaultConnectionStringName)
|
|
|
|
|
{
|
|
|
|
|
//Return tenant's default or global default
|
|
|
|
|
return !tenantDefaultConnectionString.IsNullOrWhiteSpace()
|
|
|
|
|
? tenantDefaultConnectionString
|
|
|
|
|
: Options.ConnectionStrings.Default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Requesting specific connection string...
|
|
|
|
|
var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (!connString.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
//Found for the tenant
|
|
|
|
|
return connString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tenant = await tenantStore.FindAsync(_currentTenant.Id.Value);
|
|
|
|
|
|
|
|
|
|
if (tenant?.ConnectionStrings == null)
|
|
|
|
|
{
|
|
|
|
|
return await base.ResolveAsync(connectionStringName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Requesting default connection string
|
|
|
|
|
if (connectionStringName == null)
|
|
|
|
|
{
|
|
|
|
|
return tenant.ConnectionStrings.Default ??
|
|
|
|
|
Options.ConnectionStrings.Default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Requesting specific connection string
|
|
|
|
|
var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (connString != null)
|
|
|
|
|
{
|
|
|
|
|
return connString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Requested a specific connection string, but it's not specified for the tenant.
|
|
|
|
|
* - If it's specified in options, use it.
|
|
|
|
|
* - If not, use tenant's default conn string.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (connStringInOptions != null)
|
|
|
|
|
{
|
|
|
|
|
return connStringInOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tenant.ConnectionStrings.Default ??
|
|
|
|
|
Options.ConnectionStrings.Default;
|
|
|
|
|
//Fallback to tenant's default connection string if available
|
|
|
|
|
if (!tenantDefaultConnectionString.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
return tenantDefaultConnectionString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Try to find the specific connection string for given name
|
|
|
|
|
var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (!connStringInOptions.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
return connStringInOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Fallback to the global default connection string
|
|
|
|
|
var defaultConnectionString = Options.ConnectionStrings.Default;
|
|
|
|
|
if (!defaultConnectionString.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
return defaultConnectionString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new AbpException("No connection string defined!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use ResolveAsync method.")]
|
|
|
|
|
public override string Resolve(string connectionStringName = null)
|
|
|
|
|
{
|
|
|
|
|
//No current tenant, fallback to default logic
|
|
|
|
|
if (_currentTenant.Id == null)
|
|
|
|
|
{
|
|
|
|
|
//No current tenant, fallback to default logic
|
|
|
|
|
return base.Resolve(connectionStringName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tenant = FindTenantConfiguration(_currentTenant.Id.Value);
|
|
|
|
|
|
|
|
|
|
if (tenant == null || tenant.ConnectionStrings.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
//Tenant has not defined any connection string, fallback to default logic
|
|
|
|
|
return base.Resolve(connectionStringName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tenantDefaultConnectionString = tenant.ConnectionStrings.Default;
|
|
|
|
|
|
|
|
|
|
//Requesting default connection string...
|
|
|
|
|
if (connectionStringName == null ||
|
|
|
|
|
connectionStringName == ConnectionStrings.DefaultConnectionStringName)
|
|
|
|
|
{
|
|
|
|
|
//Return tenant's default or global default
|
|
|
|
|
return !tenantDefaultConnectionString.IsNullOrWhiteSpace()
|
|
|
|
|
? tenantDefaultConnectionString
|
|
|
|
|
: Options.ConnectionStrings.Default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Requesting specific connection string...
|
|
|
|
|
var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (!connString.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
//Found for the tenant
|
|
|
|
|
return connString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Fallback to tenant's default connection string if available
|
|
|
|
|
if (!tenantDefaultConnectionString.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
return tenantDefaultConnectionString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Try to find the specific connection string for given name
|
|
|
|
|
var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (!connStringInOptions.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
return connStringInOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Fallback to the global default connection string
|
|
|
|
|
var defaultConnectionString = Options.ConnectionStrings.Default;
|
|
|
|
|
if (!defaultConnectionString.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
return defaultConnectionString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new AbpException("No connection string defined!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual async Task<TenantConfiguration> FindTenantConfigurationAsync(Guid tenantId)
|
|
|
|
|
{
|
|
|
|
|
using (var serviceScope = _serviceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var tenantStore = serviceScope
|
|
|
|
|
.ServiceProvider
|
|
|
|
|
.GetRequiredService<ITenantStore>();
|
|
|
|
|
|
|
|
|
|
return await tenantStore.FindAsync(tenantId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use FindTenantConfigurationAsync method.")]
|
|
|
|
|
protected virtual TenantConfiguration FindTenantConfiguration(Guid tenantId)
|
|
|
|
|
{
|
|
|
|
|
using (var serviceScope = _serviceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var tenantStore = serviceScope
|
|
|
|
|
.ServiceProvider
|
|
|
|
|
.GetRequiredService<ITenantStore>();
|
|
|
|
|
|
|
|
|
|
var tenant = tenantStore.Find(_currentTenant.Id.Value);
|
|
|
|
|
|
|
|
|
|
if (tenant?.ConnectionStrings == null)
|
|
|
|
|
{
|
|
|
|
|
return base.Resolve(connectionStringName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Requesting default connection string
|
|
|
|
|
if (connectionStringName == null)
|
|
|
|
|
{
|
|
|
|
|
return tenant.ConnectionStrings.Default ??
|
|
|
|
|
Options.ConnectionStrings.Default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Requesting specific connection string
|
|
|
|
|
var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (connString != null)
|
|
|
|
|
{
|
|
|
|
|
return connString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Requested a specific connection string, but it's not specified for the tenant.
|
|
|
|
|
* - If it's specified in options, use it.
|
|
|
|
|
* - If not, use tenant's default conn string.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
|
|
|
|
|
if (connStringInOptions != null)
|
|
|
|
|
{
|
|
|
|
|
return connStringInOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tenant.ConnectionStrings.Default ??
|
|
|
|
|
Options.ConnectionStrings.Default;
|
|
|
|
|
return tenantStore.Find(tenantId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|