-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStartupApplication.cs
61 lines (45 loc) · 1.77 KB
/
StartupApplication.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
using Arex388.AspNet.Mvc.Startup;
using Microsoft.Extensions.DependencyInjection;
using Owin;
using System;
using System.Web;
[assembly: PreApplicationStartMethod(typeof(StartupApplication), nameof(StartupApplication.InitModule))]
namespace Arex388.AspNet.Mvc.Startup {
public abstract class StartupApplication :
HttpApplication {
public static IServiceProvider ServiceProvider { get; private set; }
public static void InitModule() => RegisterModule(Statics.ServiceScopeModuleType);
public void Configuration(
IAppBuilder app) {
BuildServiceProvider();
app.Properties.Add(Statics.ServiceProviderKey, ServiceProvider);
Configure(app);
}
public abstract void Configure(
IAppBuilder app);
[Obsolete("ServiceProvider is now configured before Configure().")]
public void ConfigureServices() => BuildServiceProvider();
protected virtual void BuildServiceProvider() {
if (ServiceProvider != null) {
return;
}
var services = new ServiceCollection();
ConfigureServices(services);
ServiceProvider = services.BuildServiceProvider();
}
public abstract void ConfigureServices(
IServiceCollection services);
public static IServiceScope CreateScope(
HttpContext context) {
var scope = ServiceProvider.CreateScope();
context.Items[Statics.ServiceScopeType] = scope;
return scope;
}
internal static void DisposeScope(
HttpContext context) {
if (context.Items[Statics.ServiceScopeType] is IServiceScope scope) {
scope.Dispose();
}
}
}
}