### Install Mellon.MultiTenant.Azure (.NET CLI) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Installs the Mellon.MultiTenant.Azure package using the .NET CLI. ```bash dotnet add package Mellon.MultiTenant.Azure ``` -------------------------------- ### Install Mellon.MultiTenant.ConfigServer (.NET CLI) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Installs the Mellon.MultiTenant.ConfigServer package using the .NET CLI. ```bash dotnet add package Mellon.MultiTenant.ConfigServer ``` -------------------------------- ### Install Mellon.MultiTenant.Hangfire (.NET CLI) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Installs the Mellon.MultiTenant.Hangfire package using the .NET CLI. ```bash dotnet add package Mellon.MultiTenant.Hangfire ``` -------------------------------- ### Install Mellon.MultiTenant.ConfigServer (Package Manager) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Installs the Mellon.MultiTenant.ConfigServer package using the Package Manager console. ```powershell Install-Package Mellon.MultiTenant.ConfigServer ``` -------------------------------- ### Install Mellon.MultiTenant Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Instructions for installing the Mellon.MultiTenant NuGet package using Package Manager Console or the .NET CLI. ```powershell Install-Package Mellon.MultiTenant ``` ```bash dotnet add package Mellon.MultiTenant ``` -------------------------------- ### Install Mellon.MultiTenant.Azure (Package Manager) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Installs the Mellon.MultiTenant.Azure package using the Package Manager console. ```powershell Install-Package Mellon.MultiTenant.Azure ``` -------------------------------- ### Install Mellon.MultiTenant.Hangfire (Package Manager) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Installs the Mellon.MultiTenant.Hangfire package using the Package Manager console. ```powershell Install-Package Mellon.MultiTenant.Hangfire ``` -------------------------------- ### Custom Tenant Configuration Source (XML Example) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Demonstrates how to implement a custom tenant configuration source by creating a class that adheres to the ITenantConfigurationSource interface. This example shows how to load tenant settings from XML files based on tenant name and environment. ```csharp public class LocalXmlTenantSource : ITenantConfigurationSource { private readonly IHostEnvironment _hostEnvironment; public LocalTenantSource( IHostEnvironment hostEnvironment) { _hostEnvironment = hostEnvironment; } public IConfigurationBuilder AddSource( string tenant, IConfigurationBuilder builder) { builder.AddXmlFile($"appsettings.{tenant}.xml", true); builder.AddXmlFile($"appsettings.{tenant}.{_hostEnvironment.EnvironmentName}.xml", true); return builder; } } ``` -------------------------------- ### EF Core DbContext Setup with Multi-Tenancy Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures the DbContext for EF Core to use multi-tenant settings. It retrieves the tenant configuration and uses it to set the SQL Server connection string. ```csharp builder.Services.AddDbContext( (IServiceProvider serviceProvider, DbContextOptionsBuilder options) => { var configuration = serviceProvider.GetRequiredService(); options.UseSqlServer(configuration?["ConnectionStrings:DefaultConnection"]); }); ``` -------------------------------- ### Multitenant Configuration via C# API Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Demonstrates how to configure multitenant settings programmatically using the C# builder pattern. This allows for setting application name, tenant identification keys, default tenant, and loading tenant lists from settings or environment variables. ```csharp builder.Services .AddMultiTenant(options => options .WithApplicationName("customer-api") .WithHttpHeader("x-tenant-name") .WithCookie("tenant-name") .WithQueryString("tenant-name") .WithDefaultTenant("client-a") .LoadFromSettings() ); ``` -------------------------------- ### Mellon Multitenant API Configuration Methods Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Provides details on the available methods for configuring the Mellon Multitenant library via its C# API. Each method allows setting specific aspects of the multitenant behavior, such as application name, tenant identification keys, default tenant, and data sources for tenant lists. ```APIDOC WithApplicationName(string): - Set the application name WithHttpHeader(string): - Set the HTTP Header key, where the tenant name will be passed WithCookie(string): - Set the HTTP Cookie key, where the tenant name will be passed WithQueryString(string): - Set the HTTP Query String key, where the tenant name will be passed WithDefaultTenant(string): - Set for when the tenant is not defined by the caller the lib will set the tenant as the tenant defined within this property, use it just when needed 😉👍 WithSkipTenantCheckPaths(string): - Add a path that will be skipped during the tenant identification WithSkipTenantCheckPaths(params string[]): - Add paths that will be skipped during the tenant identification LoadFromSettings(): - Set for when the tenant list will be loaded from the settings **MultiTenant:Tenants** LoadFromEnvironmentVariable(): - Set for when the tenant list will be loaded from the environment variable **MULTITENANT_TENANTS** ``` -------------------------------- ### Configure Azure App Configuration with Custom Options Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures Azure App Configuration with custom options, allowing dynamic connection string and selection based on tenant. ```csharp builder.Services .AddMultiTenant() .AddMultiTenantAzureAppConfiguration(options => options.AzureAppConfigurationOptions = (serviceProvider, tenant) => { var configuration = serviceProvider.GetRequiredService(); return azureOptions => azureOptions .Connect(configuration["AzureAppConfigurationConnectionString"]) .Select("*", tenant); } ); ``` -------------------------------- ### Loading Tenants from Environment Variable (PowerShell) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Illustrates how to set the environment variable `MULTITENANT_TENANTS` to load the list of tenants. The tenants should be provided as a comma-separated string. ```powershell $Env:MULTITENANT_TENANTS = 'client-a,client-b,client-c' ``` -------------------------------- ### Configure Azure App Configuration Services Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures the application services to use Azure App Configuration for multi-tenancy. ```csharp builder.Services .AddMultiTenant() .AddMultiTenantAzureAppConfiguration(); ``` -------------------------------- ### EF Core Migrations for Multiple Tenants Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Applies EF Core database migrations for each tenant. It iterates through all registered tenants, sets the current tenant context, and then applies the migrations using MigrateAsync. ```csharp var tenants = app.Services.GetRequiredService(); foreach (var tenant in tenants.Tenants) { using (var scope = app.Services.CreateScope()) { var tenantSettings = scope.ServiceProvider.GetRequiredService(); tenantSettings.SetCurrentTenant(tenant); var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } } app.Run(); ``` -------------------------------- ### Multitenant Settings Configuration (JSON) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Defines the multitenant configuration using a JSON object. This includes application name, keys for tenant identification in HTTP headers, cookies, and query strings, as well as the source for tenant lists and specific tenant names or paths to skip. ```json { "MultiTenant": { "ApplicationName": "customer-api", "HttpHeaderKey": "x-tenant-name", "CookieKey": "tenant-name", "QueryStringKey": "tenant-name", "TenantSource": "Settings", "SkipTenantCheckPaths": ["^/swagger.*"], "Tenants": [ "client-a", "client-b", "client-c" ] } } ``` -------------------------------- ### Load Tenant List from HTTP Endpoint (Custom Logic) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Defines how the tenant list is loaded from an HTTP endpoint using a custom Func. This method allows for detailed control over the HTTP request, including authorization and response handling. It requires specifying endpoint details in the application settings. ```csharp services .AddMultiTenant(options => options.LoadFromEndpoint((endpointOptions, configuration) => { var request = new HttpRequestMessage() { RequestUri = new Uri(endpointOptions.Url), Method = new HttpMethod(endpointOptions.Method), }; if (!string.IsNullOrEmpty(endpointOptions.Authorization)) { request.Headers.Add("Authorization", endpointOptions.Authorization); } using (var client = new HttpClient()) { var result = client.Send(request); if (result.IsSuccessStatusCode) { var data = result.Content.ReadFromJsonAsync>().GetAwaiter().GetResult(); return data!.Select(x => x.Id).ToArray(); } else { var statusCode = result.StatusCode; var reason = result.ReasonPhrase; var content = result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); throw new Exception($"Error to load tenants from the url {endpointOptions.Url} StatusCode: {statusCode} Reason: {reason} Content: {content}"); } } })); ``` ```json "MultiTenant": { // other settings... "Endpoint": { "Url": "[endpoint]", "Method": "GET", "Authorization": "Basic $user $password" }, // other settings... } ``` -------------------------------- ### Load Tenant List from HTTP Endpoint (Simple) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Loads the tenant list from an HTTP endpoint using a generic type. This method simplifies the process by making a basic HTTP request to the specified endpoint, respecting the Url, Method, and Authorization settings. ```csharp services .AddMultiTenant(options => options.LoadFromEndpoint(x => x.TenantId)); ``` -------------------------------- ### Web API Tenant Configuration Access Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Demonstrates how to inject and use the IMultiTenantConfiguration interface within an ASP.NET Core endpoint to access tenant-specific settings and configuration values. ```csharp app.MapGet("", (IMultiTenantConfiguration configuration) => { return new { Tenant = configuration.Tenant, Message = configuration["Message"], }; }); ``` -------------------------------- ### Configure Hangfire Multi-Tenant Services Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures the application services to use Hangfire with multi-tenancy support. ```csharp builder.Services .AddMultiTenant() .AddMultiTenantHangfire(); ``` -------------------------------- ### Configure Spring Cloud Config Services Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures the application services to use Spring Cloud Config for multi-tenancy. ```csharp builder.Services .AddMultiTenant() .AddMultiTenantSpringCloudConfig(); ``` -------------------------------- ### Configure Hangfire with Multi-Tenant Provider Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures Hangfire to use the multi-tenant provider by passing the IServiceProvider. ```csharp builder.Services.AddHangfire((serviceProvider, config) => { // some code config.UseMultiTenant(serviceProvider); // some code }); ``` -------------------------------- ### Configure Hangfire Server with Tenant Queues Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Configures the Hangfire server to process jobs from queues named after tenants, along with additional queues. ```csharp builder.Services.AddHangfireServer((serviceProvider, config) => { var multiTenantSettings = serviceProvider.GetRequiredService(); var queues = new List(multiTenantSettings.Tenants); // if you want to add more queues queues.Add("cron"); queues.Add("default"); config.Queues = tenants.ToArray(); // some code }); ``` -------------------------------- ### Enqueue Background Jobs Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Enqueues background jobs for execution, either for the current tenant or all tenants. Supports various method call expressions and generic types. ```C# EnqueueForAllTenants(Expression methodCall) - Enqueues a job for all tenants. EnqueueForAllTenants(Expression> methodCall) - Enqueues an asynchronous job for all tenants. EnqueueForAllTenants(Expression> methodCall) - Enqueues a generic job for all tenants. EnqueueForAllTenants(Expression> methodCall) - Enqueues a generic asynchronous job for all tenants. Enqueue(Expression methodCall) - Enqueues a job for the current tenant. Enqueue(Expression> methodCall) - Enqueues an asynchronous job for the current tenant. Enqueue(Expression> methodCall) - Enqueues a generic job for the current tenant. Enqueue(Expression> methodCall) - Enqueues a generic asynchronous job for the current tenant. ``` -------------------------------- ### Add Recurring Job for All Tenants Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Adds or updates a recurring job for all tenants, specifying the job method, cron expression, and optional time zone and queue. ```csharp AddOrUpdateForAllTenants(string recurringJobId,Expression> methodCall, string cronExpression, TimeZoneInfo timeZone = null,string queue = "default") ``` -------------------------------- ### Recurring Job Management Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Manages recurring background jobs, including adding, updating, removing, and triggering them for the current or all tenants. ```C# AddOrUpdate(string recurringJobId, Job job, string cronExpression, TimeZoneInfo timeZone) - Creates or updates a recurring job for the current tenant. RemoveIfExistsForAllTenants(string recurringJobId) - Removes a recurring job for all tenants. RemoveIfExists(string recurringJobId) - Removes a recurring job for the current tenant. TriggerForAllTenants(string recurringJobId) - Enqueues a recurring job for all tenants. Trigger(string recurringJobId) - Enqueues a recurring job for the current tenant. ``` -------------------------------- ### Web API Service Registration Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Registers the necessary multi-tenant services for an ASP.NET Core Web API application. This is the initial step to enable multi-tenancy features. ```csharp builder.Services.AddMultiTenant(); ``` -------------------------------- ### Schedule Background Jobs Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Schedules background jobs for delayed execution, either for the current tenant or all tenants. Supports various method call expressions and generic types. ```C# ScheduleForAllTenants(Expression methodCall, TimeSpan delay) - Schedules a job for all tenants with a specified delay. ScheduleForAllTenants((Expression> methodCall, TimeSpan delay) - Schedules an asynchronous job for all tenants with a specified delay. ScheduleForAllTenants(Expression> methodCall, TimeSpan delay) - Schedules a generic job for all tenants with a specified delay. ScheduleForAllTenants(Expression> methodCall, TimeSpan delay) - Schedules a generic asynchronous job for all tenants with a specified delay. Schedule(Expression methodCall, TimeSpan delay) - Schedules a job for the current tenant with a specified delay. Schedule(Expression> methodCall, TimeSpan delay) - Schedules an asynchronous job for the current tenant with a specified delay. Schedule(Expression> methodCall, TimeSpan delay) - Schedules a generic job for the current tenant with a specified delay. Schedule(Expression> methodCall, TimeSpan delay) - Schedules a generic asynchronous job for the current tenant with a specified delay. ``` -------------------------------- ### Settings Refresh Endpoint Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Provides endpoints to refresh application settings for all tenants or a specific tenant without requiring an application restart. This functionality is dependent on AzureAppConfiguration or SpringCloudConfig. ```APIDOC GET /refresh-settings Description: Refreshes settings for all tenants. Dependencies: AzureAppConfiguration or SpringCloudConfig GET /refresh-settings/{tenantName} Description: Refreshes settings for a specific tenant. Parameters: - tenantName (path): The name of the tenant whose settings should be refreshed. Dependencies: AzureAppConfiguration or SpringCloudConfig ``` -------------------------------- ### Web API Tenant Middleware Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Registers the multi-tenant middleware in the ASP.NET Core request pipeline. This middleware is responsible for identifying the current tenant based on the HttpContext. ```csharp app.UseMultiTenant(); ``` -------------------------------- ### Add or Update Recurring Job for All Tenants (Job Object) Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Adds or updates a recurring job for all tenants using a Job object and specifying the cron expression and time zone. ```csharp AddOrUpdateForAllTenants(string recurringJobId, Job job, string cronExpression, TimeZoneInfo timeZone) ``` -------------------------------- ### Add or Update Recurring Job for Current Tenant Source: https://github.com/pub-dev/mellon.multitenant/blob/main/README.md Adds or updates a recurring job for the current tenant, specifying the job method, cron expression, and optional time zone and queue. ```csharp AddOrUpdate(string recurringJobId,Expression> methodCall, string cronExpression, TimeZoneInfo timeZone = null,string queue = "default") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.