Eodg.MultiTenancy.Middleware
0.5.0-alpha
dotnet add package Eodg.MultiTenancy.Middleware --version 0.5.0-alpha
NuGet\Install-Package Eodg.MultiTenancy.Middleware -Version 0.5.0-alpha
<PackageReference Include="Eodg.MultiTenancy.Middleware" Version="0.5.0-alpha" />
paket add Eodg.MultiTenancy.Middleware --version 0.5.0-alpha
#r "nuget: Eodg.MultiTenancy.Middleware, 0.5.0-alpha"
// Install Eodg.MultiTenancy.Middleware as a Cake Addin #addin nuget:?package=Eodg.MultiTenancy.Middleware&version=0.5.0-alpha&prerelease // Install Eodg.MultiTenancy.Middleware as a Cake Tool #tool nuget:?package=Eodg.MultiTenancy.Middleware&version=0.5.0-alpha&prerelease
MultiTenancyMiddleware
It's a little black box... kind of
This library allows you to incorporate multitenancy into a .net core 2.0 web api project. After setting up a handful of things (outlined below), you will be able to make a request, and by adding a header to said request containing the account id of the tenant for whose data you want to access you can get said data.
How do you implement it?
- Write a concrete implementation of the provided
MultiTenancyResolverService
abstract class /IMultiTenancyResolverService
interface.- There only has to be a
ConnectionString
property and aSetConnectionStringByAccountId(string accountId)
method that sets theConnectionString
property.
- There only has to be a
- In the
Startup.cs
file, we'll have to do a couple of things:Add your implementation to the built-in DI container a la:
services.AddScoped<IMultiTenancyResolverService, ExampleMultiTenancyResolverService>();
where
ExampleMultiTenancyResolverService
is the name of your concrete implementation of the providedMultiTenancyResolverService
abstract class /IMultiTenancyResolverService
interfaceAdd the options for the middleware. This only contains a
UseHeaderKey
property and aHeaderKey
property and this will be the header key used in the request that contains the account id of the tenant whose database you want to reference for the particular request... Or setUseHeaderKey
tofalse
if your code will be doing things a different way.var multiTenancyMiddlewareOptions = new MultiTenancyMiddlewareOptions { UseHeaderKey = true, TenantIdHeaderKey = "Account-Id" }; services.AddSingleton(multiTenancyMiddlewareOptions);
- Database context class for the tenant template database:
Inject the
IMultiTenancyResolver
into the constructor, and store in private variable:private IMultiTenancyResolverService _multiTenancyResolverService;
Add the following method:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(_multiTenancyResolverService.ConnectionString); base.OnConfiguring(optionsBuilder); }
How do you use it?
For any controller/method utilizing multitenancy add the following attribute
[MiddlewareFilter(typeof(MultiTenancyPipeline))]
Any time a request is made to said controllers/methods, a header will have to exist on the request that has the key specified in the
Startup.cs
class, and will have to have a value of the account id.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.0
- Microsoft.AspNetCore (>= 2.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.5.0-alpha | 967 | 4/4/2018 |
0.4.0-alpha | 663 | 4/4/2018 |
0.3.0-aplha | 699 | 4/3/2018 |
0.2.0-aplha | 839 | 3/31/2018 |
0.1.0-aplha | 849 | 3/31/2018 |