-
Notifications
You must be signed in to change notification settings - Fork 79
Add multi-tenant guidance for .NET #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19b4306
add multitenant example instructions
amerjusupovic b083c47
fix spacing
amerjusupovic 6237da6
update spacing
amerjusupovic 16b55a3
move file
amerjusupovic 0caf98e
rename to readme
amerjusupovic 9e970de
update names in example
amerjusupovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Multi-tenant application setup | ||
|
|
||
| The .NET Azure App Configuration Provider delivers configuration settings to the existing IConfiguration system. Multi-tenant applications using the provider can take advantage of IConfiguration features to retrieve tenant information from requests. The following instructions will demonstrate one way to achieve this application setup. | ||
|
|
||
| 1. Define the per tenant settings in App Configuration as key-values using key prefixes. | ||
|
|
||
| #### Tenant 1 | ||
|
|
||
| | Key | Value | | ||
| |---- |-------| | ||
| | Contoso:Name | Contoso Corp. | | ||
| | Contoso:Color | Azure | | ||
|
|
||
| #### Tenant 2 | ||
|
|
||
| | Key | Value | | ||
| |---- |-------| | ||
| | Fabrikam:Name | Fabrikam, Inc. | | ||
| | Fabrikam:Color | Green | | ||
|
|
||
| 2. Setup the options pattern by creating the following classes. | ||
|
|
||
| ```cs | ||
| // | ||
| // Tenant info | ||
| // | ||
| public class TenantSettings | ||
| { | ||
| public string Name { get; set; } | ||
| public string Color { get; set; } | ||
| } | ||
| ``` | ||
|
|
||
| ```cs | ||
| // | ||
| // Dynamically configure TenantSettings | ||
| // | ||
| public class ConfigureTenantSettings : IConfigureOptions<TenantSettings> | ||
| { | ||
| private readonly IConfiguration _configuration; | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
|
||
| public ConfigureTenantSettings( | ||
| IConfiguration configuration, | ||
| IHttpContextAccessor httpContextAccessor) | ||
| { | ||
| _configuration = configuration; | ||
| _httpContextAccessor = httpContextAccessor; | ||
| } | ||
|
|
||
| public void Configure(TenantSettings options) | ||
| { | ||
| // | ||
| // Get tenant id from the request | ||
| // ex. Read tenant from a request header | ||
| if (!_httpContextAccessor.HttpContext.Request.Headers.TryGetValue( | ||
| "X-Tenant-Id", | ||
| out StringValues tenantId)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // | ||
| // Initialize from config section by TenantId | ||
| _configuration.GetSection(tenantId).Bind(options); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 3. Register the options as a scoped DI service. | ||
|
|
||
| ```cs | ||
| services.AddScoped<IConfigureOptions<TenantSettings>, ConfigureTenantSettings>(); | ||
| ``` | ||
|
|
||
| 4. Use in a DI Service or Controller via `IOptionsSnapshot`. | ||
|
|
||
| ```cs | ||
| public class MyController : Controller | ||
| { | ||
| private readonly IOptionsSnapshot<TenantSettings> _tenantSettings; | ||
|
|
||
| public MyController(IOptionsSnapshot<TenantSettings> tenantSettings) | ||
|
jimmyca15 marked this conversation as resolved.
|
||
| { | ||
| _tenantSettings = tenantSettings; | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public IActionResult Get() | ||
| { | ||
| TenantSettings tenantSettings = _tenantSettings.Value; | ||
|
|
||
| // ... | ||
| } | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.