-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAspNetCoreServiceProvider.cs
More file actions
118 lines (97 loc) · 3.32 KB
/
AspNetCoreServiceProvider.cs
File metadata and controls
118 lines (97 loc) · 3.32 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
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
// ReSharper disable once CheckNamespace
namespace APIology.ServiceProvider
{
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Configuration;
using Topshelf;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.IO;
using Core;
[SuppressMessage("ReSharper", "MemberCanBeProtected.Global")]
public abstract class AspNetCoreServiceProvider<TAPIBase, TConfiguration> : BaseServiceProvider<TConfiguration>
where TAPIBase : BaseServiceProvider<TConfiguration>
where TConfiguration : AspNetCoreConfiguration, new()
{
private IWebHost _instance;
public IHostingEnvironment Environment { get; private set; }
public override bool Start(HostControl hostControl)
{
Logger.Debug("Building web server configuration");
Logger.Debug("Starting web server");
Startup.Service = this;
_instance = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
#if NETCORE
.UseSerilog()
#endif
.Start(Config.Bindings.Select(bc => bc.BoundUri).ToArray());
var registeredAddress = _instance.ServerFeatures.Get<IServerAddressesFeature>();
Logger.Information("Web server has been bound to {Addresses}", registeredAddress.Addresses);
return true;
}
public class Startup
{
internal static AspNetCoreServiceProvider<TAPIBase, TConfiguration> Service;
private static ILifetimeScope Container { get; set; }
public Startup(IHostingEnvironment env)
{
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
Service.ConfigureAspNetCore(services);
services.AddLogging();
var builder = new ContainerBuilder();
builder.Populate(services);
Service.BuildAspNetCoreDependencySubcontainer(builder);
Container = builder.Build();
return new AutofacServiceProvider(Container);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
Service.BuildAspNetCoreApp(app, env);
appLifetime.ApplicationStopped.Register(() => Container.Dispose());
}
}
public override bool Stop(HostControl hostControl)
{
Logger.Debug("Web server stopping");
if (ReferenceEquals(_instance, null))
return false;
_instance.Dispose();
_instance = null;
return true;
}
public override void BuildDependencyContainer(ContainerBuilder builder)
{
builder.RegisterType<LoggerFactory>()
.As<ILoggerFactory>()
.OnActivating(args => {
args.Instance.AddSerilog();
})
.SingleInstance();
}
public virtual void BuildAspNetCoreDependencySubcontainer(ContainerBuilder builder)
{
}
public virtual void AspNetCoreStartupHandler(IHostingEnvironment env)
{
throw new NotImplementedException();
}
public virtual void ConfigureAspNetCore(IServiceCollection services)
{
throw new NotImplementedException();
}
public abstract void BuildAspNetCoreApp(IApplicationBuilder app, IHostingEnvironment env);
}
}