-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStartup.cs
More file actions
92 lines (81 loc) · 3.81 KB
/
Startup.cs
File metadata and controls
92 lines (81 loc) · 3.81 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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Todo.Persistence;
using Todo.Services;
using Todo.WebApi.Logging;
namespace Todo.WebApi
{
/// <summary>
/// Starts this ASP.NET Core application.
/// </summary>
public class Startup
{
/// <summary>
/// Creates a new instance of the <see cref="Startup"/> class.
/// </summary>
/// <param name="configuration">The configuration to be used for setting up this application.</param>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
private IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Configure logging
services.AddLogging(loggingBuilder =>
{
// https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
var log4NetProviderOptions = Configuration.GetSection("Log4NetCore").Get<Log4NetProviderOptions>();
loggingBuilder.AddLog4Net(log4NetProviderOptions);
// https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore#net-core-20---logging-debug-level-messages
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
});
// Configure EF Core
services.AddDbContext<TodoDbContext>((serviceProvider, dbContextOptionsBuilder) =>
{
var connectionString = Configuration.GetConnectionString("Todo");
dbContextOptionsBuilder.UseNpgsql(connectionString)
.EnableSensitiveDataLogging()
.UseLoggerFactory(serviceProvider.GetRequiredService<ILoggerFactory>());
});
// Configure ASP.NET Web API
services.AddMvc(options =>
{
// In case the client requests data in an unsupported format, respond with 406 status code
options.ReturnHttpNotAcceptable = true;
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Configure application services
services.AddScoped<ITodoService, TodoService>();
services.AddScoped<IDatabaseSeeder, DatabaseSeeder>();
// Register service with 2 interfaces.
// See more here: https://andrewlock.net/how-to-register-a-service-with-multiple-interfaces-for-in-asp-net-core-di/.
services.AddSingleton<LoggingService>();
services.AddSingleton<IHttpObjectConverter>(x => x.GetRequiredService<LoggingService>());
services.AddSingleton<IHttpContextLoggingHandler>(x => x.GetRequiredService<LoggingService>());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Ensure logging middleware is invoked as early as possible
applicationBuilder.UseHttpLogging();
if (environment.IsDevelopment())
{
applicationBuilder.UseDeveloperExceptionPage();
applicationBuilder.UseDatabaseErrorPage();
}
else
{
applicationBuilder.UseHsts();
}
applicationBuilder.UseHttpsRedirection()
.UseMvc();
}
}
}