-
Notifications
You must be signed in to change notification settings - Fork 0
/
OltServiceCollectionAspnetCoreExtensions.cs
119 lines (105 loc) · 5.07 KB
/
OltServiceCollectionAspnetCoreExtensions.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualBasic;
namespace OLT.Core
{
public static class OltServiceCollectionAspnetCoreExtensions
{
/// <summary>
/// Build Default AspNetCore Service and configures Dependency Injection
/// </summary>
/// <param name="services"></param>
/// <param name="action">Invoked after initialized</param>
/// <returns></returns>
public static IServiceCollection AddOltAspNetCore(this IServiceCollection services, Action<IMvcBuilder>? action = null)
{
return AddOltAspNetCore(services, new OltInjectionAssemblyFilter(), action);
}
/// <summary>
/// Build Default AspNetCore Service and configures Dependency Injection
/// </summary>
/// <param name="services"></param>
/// <param name="action">Invoked after initialized</param>
/// <param name="filter">Assembly Filter</param>
/// <returns></returns>
public static IServiceCollection AddOltAspNetCore(this IServiceCollection services, OltInjectionAssemblyFilter filter, Action<IMvcBuilder>? action = null)
{
return AddOltAspNetCore(services, new List<Assembly>(), filter, action);
}
/// <summary>
/// Build Default AspNetCore Service and configures Dependency Injection
/// </summary>
/// <param name="services"></param>
/// <param name="baseAssembly">Assembly to include in scan for interfaces</param>
/// <param name="action">Invoked after initialized</param>
/// <returns></returns>
public static IServiceCollection AddOltAspNetCore(this IServiceCollection services, Assembly baseAssembly, Action<IMvcBuilder>? action = null)
{
return AddOltAspNetCore(services, baseAssembly, new OltInjectionAssemblyFilter(), action);
}
/// <summary>
/// Build Default AspNetCore Service and configures Dependency Injection
/// </summary>
/// <param name="services"></param>
/// <param name="baseAssembly">Assembly to include in scan for interfaces</param>
/// <param name="action">Invoked after initialized</param>
/// <param name="filter">Assembly Filter</param>
/// <returns></returns>
public static IServiceCollection AddOltAspNetCore(this IServiceCollection services, Assembly baseAssembly, OltInjectionAssemblyFilter filter, Action<IMvcBuilder>? action = null)
{
if (baseAssembly == null)
{
throw new ArgumentNullException(nameof(baseAssembly));
}
return AddOltAspNetCore(services, new List<Assembly>() { baseAssembly }, filter, action);
}
/// <summary>
/// Build Default AspNetCore Service and configures Dependency Injection
/// </summary>
/// <param name="services"></param>
/// <param name="baseAssemblies">List of assemblies to include in scan for interfaces</param>
/// <param name="action">Invoked after initialized</param>
/// <returns></returns>
public static IServiceCollection AddOltAspNetCore(this IServiceCollection services, List<Assembly> baseAssemblies, Action<IMvcBuilder>? action = null)
{
return AddOltAspNetCore(services, baseAssemblies, new OltInjectionAssemblyFilter(), action);
}
/// <summary>
/// Build Default AspNetCore Service and configures Dependency Injection
/// </summary>
/// <param name="services"></param>
/// <param name="baseAssemblies">List of assemblies to include in scan for interfaces</param>
/// <param name="action">Invoked after initialized</param>
/// <param name="filter">Assembly Filter</param>
/// <returns></returns>
public static IServiceCollection AddOltAspNetCore(this IServiceCollection services, List<Assembly> baseAssemblies, OltInjectionAssemblyFilter filter, Action<IMvcBuilder>? action = null)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (baseAssemblies == null)
{
baseAssemblies = new List<Assembly>();
}
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
{
baseAssemblies.Add(entryAssembly);
}
baseAssemblies.Add(Assembly.GetExecutingAssembly());
services
.AddCors(baseAssemblies)
.AddOltInjection(baseAssemblies, filter)
.AddSingleton<IOltHostService, OltHostAspNetCoreService>()
.AddScoped<IOltIdentity, OltIdentityAspNetCore>()
.AddScoped<IOltDbAuditUser>(x => x.GetRequiredService<IOltIdentity>())
.AddHttpContextAccessor();
var mvcBuilder = services.AddControllers();
action?.Invoke(mvcBuilder);
return services;
}
}
}