Class SessionServiceCollectionExtensions
- Namespace
- Microsoft.Extensions.DependencyInjection
- Assembly
- Stratara.Sessions.dll
DI extensions for Stratara's session-context provider.
public static class SessionServiceCollectionExtensions
- Inheritance
-
SessionServiceCollectionExtensions
- Inherited Members
Methods
AddSessionContext(IServiceCollection)
Register the concrete ISessionContextProvider implementation as
a scoped service and register SessionContextOptions, read from the
SessionContext configuration section. Pair with
app.UseMiddleware<SessionContextMiddleware>() in the ASP.NET Core
pipeline to populate the context per request.
public static IServiceCollection AddSessionContext(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe service collection to mutate.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
Registers the scoped context and its accessor. In an ASP.NET host, add the middleware too — without it the context stays empty for every request:
// appsettings.json: { "SessionContext": { "AllowTenantHeader": false } }
services.AddSessionContext();
app.UseMiddleware<SessionContextMiddleware>();
Remarks
The section is read from the IConfiguration the container holds when the options are
first resolved, so a host built with Host.CreateApplicationBuilder or
WebApplication.CreateBuilder needs no further code. A service collection that holds no
configuration gets the defaults.
A value configured in code with services.Configure<SessionContextOptions>(...) after
this call takes precedence over the section; one configured before it is overwritten for every
key the section carries. The section is applied once, at the position of the first call, so
calling this method again — directly or through a composite — does not re-apply it.