-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionInit.cs
More file actions
executable file
·57 lines (52 loc) · 2.61 KB
/
SessionInit.cs
File metadata and controls
executable file
·57 lines (52 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Microsoft.Extensions.DependencyInjection;
using BlazorSessionProvider.Sessions;
using BlazorSessionProvider.Sessions.Binding;
namespace BlazorSessionProvider
{
/// <summary>
///
/// </summary>
public static class SessionInit
{
/// <summary>
/// Adds an ISessionProvider to the services. This method implements a HttpContextAccessor in the service collection for the SessionBridge.
/// </summary>
/// <param name="services"></param>
/// <param name="configuration">Configuration object</param>
public static void AddSessionProvider(this IServiceCollection services, Action<SessionProviderConfig>? configuration = null)
{
if (services.Any(srv => srv.ServiceType == typeof(ISessionKeeper)))
return;
services.Configure<SessionProviderConfig>(configuration == null ? config => { } : configuration);
services.AddSingleton<ISessionKeeper, SessionKeeper>();
services.AddHttpContextAccessor();
services.AddScoped<ISessionBridge, SessionBridge>();
services.AddScoped<ISessionProvider, SessionProvider>();
}
/// <summary>
/// Adds a session provider to the services, with the specified SessionBridge Scoped. This method does not implements a HttpContextAccessor.
/// </summary>
/// <param name="services"></param>
/// <param name="configuration">Configuration object</param>
/// <typeparam name="B">Bridge scoped class</typeparam>
public static void AddSessionProvider<B>(this IServiceCollection services, Action<SessionProviderConfig>? configuration = null) where B : ISessionBridge
{
if (services.Any(srv => srv.ServiceType == typeof(ISessionKeeper)))
return;
services.Configure<SessionProviderConfig>(configuration == null ? config => { } : configuration);
services.AddSingleton<ISessionKeeper, SessionKeeper>();
services.AddScoped(typeof(ISessionBridge), typeof(B));
services.AddScoped<ISessionProvider, SessionProvider>();
}
/// <summary>
///
/// </summary>
/// <typeparam name="B"></typeparam>
/// <param name="services"></param>
public static void AddBindingService<B>(this IServiceCollection services) where B : SessionBinder, new()
{
if (!services.Any(srv => srv.ServiceType == typeof(SessionBindingService<B>)))
services.AddScoped<SessionBindingService<B>>();
}
}
}