forked from ardalis/CleanArchitecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
125 lines (107 loc) · 4.46 KB
/
Startup.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
120
121
122
123
124
125
using Autofac;
using Autofac.Extensions.DependencyInjection;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.SharedKernel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace CleanArchitecture.Web
{
public class Startup
{
public Startup(IConfiguration config)
{
Configuration = config;
}
public IConfiguration Configuration { get; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc()
.AddControllersAsServices()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
AddDbContextToServices(services);
return BuildDependencyInjectionProvider(services);
}
private void AddDbContextToServices(IServiceCollection services)
{
// Create lightweight container just so we can get IDatabaseRegistrar instance.
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(GetAssembliesToRegister(false)).AsImplementedInterfaces();
IDatabaseRegistrar databaseRegistrar = builder.Build().Resolve<IDatabaseRegistrar>();
databaseRegistrar.RegisterSQLite(services);
// databaseRegistrar.RegisterInMemory(services);
// databaseRegistrar.RegisterSQLServer(services, Configuration.GetConnectionString("DefaultConnection"));
}
private static IServiceProvider BuildDependencyInjectionProvider(IServiceCollection services)
{
ContainerBuilder builder = new ContainerBuilder();
// Populate the container using the service collection
builder.Populate(services);
builder.RegisterAssemblyTypes(GetAssembliesToRegister()).AsImplementedInterfaces();
IContainer applicationContainer = builder.Build();
return new AutofacServiceProvider(applicationContainer);
}
private static Assembly[] GetAssembliesToRegister(bool includeWeb = true)
{
List<Assembly> assemblies = new List<Assembly>();
if (includeWeb)
{
assemblies.Add(Assembly.GetExecutingAssembly());
}
assemblies.Add(Assembly.GetAssembly(typeof(BaseEntity)));
assemblies.Add(GetInfrastructureAssembly());
return assemblies.ToArray();
}
private static Assembly GetInfrastructureAssembly()
{
string location = AppDomain.CurrentDomain.BaseDirectory;
const string infrastructureDll = "CleanArchitecture.Infrastructure.dll";
return Assembly.LoadFile($"{location}{infrastructureDll}");
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}