#### [Abblix\.Oidc\.Server](https://www.abblix.com/docs/api/abblix-oidc-server 'index')
### [Abblix\.Oidc\.Server\.Features\.MultiTenancy](https://www.abblix.com/docs/api/abblix-oidc-server/Abblix.Oidc.Server.Features.MultiTenancy 'Abblix\.Oidc\.Server\.Features\.MultiTenancy')

## TenantScope Class

Runs work of one tenant outside a request resolved to it, for a service that takes no tenant of its own \- a
security event dispatched from a message consumer, for one\.

```csharp
public sealed class TenantScope : System.IDisposable
```

Inheritance [System\.Object](https://learn.microsoft.com/en-us/dotnet/api/system.object 'System\.Object') → TenantScope

Implements [System\.IDisposable](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable 'System\.IDisposable')

### Remarks
The tenant accessor answers with the tenant of the innermost scope entered here and, outside any, with the tenant
the request was resolved to; entered within a request, a scope takes precedence over it\. A service reading the
tenant from the accessor therefore serves the tenant entered here, while a service whose method takes the tenant
is handed the definition and needs no scope\. The scope flows across awaits and ends when disposed:

```csharp
var tenant = await catalog.FindByIdAsync("acme", cancellationToken)
    ?? throw new InvalidOperationException("No tenant is registered under the id acme.");

using (TenantScope.Enter(tenant))
    await dispatcher.DispatchAsync(descriptor, cancellationToken);
```
### Properties

## TenantScope\.Current Property {#Abblix.Oidc.Server.Features.MultiTenancy.TenantScope.Current}

The tenant of the innermost scope entered on this flow of execution, or null outside any\.

```csharp
public static Abblix.Oidc.Server.Features.MultiTenancy.TenantContext? Current { get; }
```

#### Property Value
[TenantContext](https://www.abblix.com/docs/api/abblix-oidc-server/Abblix.Oidc.Server.Features.MultiTenancy.TenantContext 'Abblix\.Oidc\.Server\.Features\.MultiTenancy\.TenantContext')
### Methods

## TenantScope\.Dispose\(\) Method {#Abblix.Oidc.Server.Features.MultiTenancy.TenantScope.Dispose()}

Ends this scope, making current the nearest scope around it that has not ended; a second call changes
nothing\.

```csharp
public void Dispose();
```

Implements [Dispose\(\)](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose 'System\.IDisposable\.Dispose')

### Remarks
A scope ended while one entered inside it is still open stays current for that inner scope's work, and is
passed over when the inner one ends: restoring its tenant then would run later work as a tenant whose scope
is over\. It does not throw, since a Dispose that throws while an exception is already leaving a
`using` block would replace that exception\.

Work started inside a scope carries the scope with it, and when that work outlives the scope it keeps
running as its tenant until it enters and ends a scope of its own; await such work before ending the scope.

## TenantScope\.Enter\(TenantDefinition\) Method {#Abblix.Oidc.Server.Features.MultiTenancy.TenantScope.Enter(Abblix.Oidc.Server.Features.MultiTenancy.TenantDefinition)}

Makes [tenant](https://www.abblix.com/docs/api/abblix-oidc-server/Abblix.Oidc.Server.Features.MultiTenancy.TenantScope#Abblix.Oidc.Server.Features.MultiTenancy.TenantScope.Enter(Abblix.Oidc.Server.Features.MultiTenancy.TenantDefinition).tenant 'Abblix\.Oidc\.Server\.Features\.MultiTenancy\.TenantScope\.Enter\(Abblix\.Oidc\.Server\.Features\.MultiTenancy\.TenantDefinition\)\.tenant') the current tenant until the returned scope is disposed\.

```csharp
public static Abblix.Oidc.Server.Features.MultiTenancy.TenantScope Enter(Abblix.Oidc.Server.Features.MultiTenancy.TenantDefinition tenant);
```
#### Parameters

###### `tenant` [TenantDefinition](https://www.abblix.com/docs/api/abblix-oidc-server/Abblix.Oidc.Server.Features.MultiTenancy.TenantDefinition 'Abblix\.Oidc\.Server\.Features\.MultiTenancy\.TenantDefinition') {#Abblix.Oidc.Server.Features.MultiTenancy.TenantScope.Enter(Abblix.Oidc.Server.Features.MultiTenancy.TenantDefinition).tenant}

#### Returns
[TenantScope](https://www.abblix.com/docs/api/abblix-oidc-server/Abblix.Oidc.Server.Features.MultiTenancy.TenantScope 'Abblix\.Oidc\.Server\.Features\.MultiTenancy\.TenantScope')

### Remarks
Call it where the work runs, not inside an async method that returns the scope: the current tenant flows into
the work awaited after the call, never back out of an async method to its caller, so a helper that awaits
something, enters the scope and returns it leaves its caller with no tenant\.
