mirror of https://github.com/abpframework/abp.git
15 changed files with 106 additions and 72 deletions
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public interface ITenantResolveContributer |
||||
|
{ |
||||
|
void Resolve(ITenantResolveContext context); |
||||
|
} |
||||
|
} |
||||
@ -1,7 +1,16 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
|
||||
namespace Volo.Abp.MultiTenancy |
namespace Volo.Abp.MultiTenancy |
||||
{ |
{ |
||||
public interface ITenantResolver |
public interface ITenantResolver |
||||
{ |
{ |
||||
void Resolve(ITenantResolveContext context); |
/// <summary>
|
||||
|
/// Tries to resolve current tenant using registered <see cref="ITenantResolveContributer"/> implementations.
|
||||
|
/// </summary>
|
||||
|
/// <returns>
|
||||
|
/// Tenant id, unique name or null (if could not resolve).
|
||||
|
/// </returns>
|
||||
|
[CanBeNull] |
||||
|
string ResolveTenantIdOrName(); |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class TenantResolver : ITenantResolver, ITransientDependency |
||||
|
{ |
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
private readonly TenantResolveOptions _options; |
||||
|
|
||||
|
public TenantResolver(IOptions<TenantResolveOptions> options, IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
_options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public string ResolveTenantIdOrName() |
||||
|
{ |
||||
|
if (!_options.TenantResolvers.Any()) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
using (var serviceScope = _serviceProvider.CreateScope()) |
||||
|
{ |
||||
|
var context = new TenantResolveContext(serviceScope.ServiceProvider); |
||||
|
|
||||
|
foreach (var tenantResolver in _options.TenantResolvers) |
||||
|
{ |
||||
|
tenantResolver.Resolve(context); |
||||
|
|
||||
|
if (context.HasResolvedTenantOrHost()) |
||||
|
{ |
||||
|
return context.TenantIdOrName; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Could not find a tenant
|
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue