MASA Framework’s Isolation building block enables your services to serve multiple tenants and environments from a single deployment. By combining middleware-based context resolution with automatic EF Core query filters, tenant and environment boundaries are enforced transparently — your business logic stays clean while data partitioning happens at the infrastructure layer.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/masastack/MASA.Framework/llms.txt
Use this file to discover all available pages before exploring further.
Installation
Install the core package plus whichever isolation strategies you need:Core Abstractions
TheIIsolation<TKey> interface is the combined contract that extends both IMultiTenant<TKey> and IMultiEnvironment. A convenience alias IIsolation binds TKey to Guid, which covers the most common scenario:
| Interface | Description |
|---|---|
IMultiTenant<TKey> | Carries the tenant identifier of type TKey |
IMultiEnvironment | Carries the current environment name |
IIsolation<TKey> | Combines both; shorthand IIsolation = IIsolation<Guid> |
Multi-Tenant Components
Masa.Contrib.Isolation.MultiTenant ships the following building blocks for resolving the active tenant on every request:
MultiTenantMiddleware
MultiTenantMiddleware
ASP.NET Core middleware that inspects each incoming HTTP request and extracts the tenant ID from the configured source — HTTP header, query string, or route value — then stores it in
MultiTenantContext for the duration of the request.MultiTenantContext
MultiTenantContext
A scoped context object that holds the resolved tenant information. Inject it wherever you need to read the current tenant within a request.
MultiTenantOptions
MultiTenantOptions
Configuration object that controls how the tenant ID is parsed — which header name, query parameter, or route segment to inspect.
CurrentUserTenantParseProvider
CurrentUserTenantParseProvider
An alternative parse provider that resolves the tenant from the authenticated user’s claims, useful when users are permanently bound to a tenant.
Multi-Environment Components
Masa.Contrib.Isolation.MultiEnvironment provides symmetric support for environment-aware routing:
MultiEnvironmentMiddleware
MultiEnvironmentMiddleware
Extracts the target environment name from the incoming request and populates
MultiEnvironmentContext.MultiEnvironmentContext
MultiEnvironmentContext
Scoped context that exposes the current environment name throughout the request pipeline.
CurrentUserEnvironmentParseProvider
CurrentUserEnvironmentParseProvider
Resolves the environment from user claims, allowing per-user environment pinning.
MasaAppConfigureParserProvider
MasaAppConfigureParserProvider
Reads the environment from the application’s own configuration, acting as a fallback when the request carries no explicit environment signal.
Registration
UseIIsolationBuilder for fluent configuration during startup, then activate the middleware in your request pipeline:
Marking Entities
To participate in automatic query filtering, your EF Core entities must implement the appropriate isolation interface. The framework then applies global query filters so that queries only return rows belonging to the current tenant or environment — no extraWhere clauses required in your repositories.
IMultiEnvironment, and for entities that require both simply implement IIsolation<Guid>:
EventBus Integration
When your application uses the MASA EventBus, isolation context must flow across event boundaries.IsolationEventMiddleware is an EventBus middleware that automatically propagates the current tenant ID and environment name into outbound events and restores them when handling inbound events — ensuring consistency in event-driven workflows without any manual context forwarding.
EF Core Query Filters
Once yourDbContext is configured with the MASA EF Core integration and your entities implement the isolation interfaces, global query filters are applied automatically:
The automatic query filter reads the tenant ID and environment name from the ambient
MultiTenantContext / MultiEnvironmentContext at query time. If no context is set (e.g. in a background job), the filter is bypassed — ensure you seed isolation context explicitly in those scenarios.How It Works End-to-End
Request arrives
MultiTenantMiddleware and/or MultiEnvironmentMiddleware inspect the request and populate the scoped context objects.Business logic executes
Services and repositories operate normally. Injected
DbContext instances transparently apply query filters based on the ambient context.Events are published
IsolationEventMiddleware captures the current isolation context and embeds it in the event metadata.