-
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathMultiTenancyServiceCollectionExtensions.cs
More file actions
59 lines (52 loc) · 2.4 KB
/
MultiTenancyServiceCollectionExtensions.cs
File metadata and controls
59 lines (52 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using BotSharp.Abstraction.MultiTenancy;
using BotSharp.Abstraction.MultiTenancy.Options;
using BotSharp.Plugin.MultiTenancy.Models;
using BotSharp.Plugin.MultiTenancy.MultiTenancy;
using BotSharp.Plugin.MultiTenancy.MultiTenancy.Resolvers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
namespace BotSharp.Plugin.MultiTenancy.Extensions;
public static class MultiTenancyServiceCollectionExtensions
{
public static IServiceCollection AddMultiTenancy(this IServiceCollection services, IConfiguration configuration, string sectionName = "TenantStore")
{
var section = configuration.GetSection(sectionName);
if (section.Exists())
{
services.Configure<TenantStoreOptions>(section);
}
else
{
services.Configure<TenantStoreOptions>(_ => { });
}
services.Configure<TenantResolveOptions>(options =>
{
options.TenantResolvers.Add(new ClaimsTenantResolveContributor());
options.TenantResolvers.Add(new HeaderTenantResolveContributor());
options.TenantResolvers.Add(new QueryStringTenantResolveContributor());
});
services.AddScoped<ITenantResolver, TenantResolver>();
services.AddScoped<MultiTenancyMiddleware>();
services.AddScoped<ICurrentTenant, CurrentTenant>();
services.AddSingleton<ICurrentTenantAccessor>(AsyncLocalCurrentTenantAccessor.Instance);
// tenant store infrastructure
services.AddMemoryCache();
services.TryAddScoped<ITenantRepository, NullTenantRepository>();
services.TryAddScoped<ConfigTenantStore>();
services.TryAddScoped<DbTenantStore>();
services.TryAddScoped<ITenantStore>(sp => new CompositeTenantStore(
sp.GetRequiredService<IOptionsMonitor<TenantStoreOptions>>(),
new List<ITenantStore>
{
sp.GetRequiredService<ConfigTenantStore>(),
sp.GetRequiredService<DbTenantStore>()
}));
services.TryAddScoped<IConnectionStringResolver, DefaultConnectionStringResolver>();
services.TryAddScoped<ITenantFeature, TenantFeature>();
services.TryAddScoped<ITenantConnectionProvider, TenantConnectionProvider>();
return services;
}
}