-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathStartup.cs
110 lines (92 loc) · 4.5 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
using CoreEx.Database;
using CoreEx.Events;
namespace My.Hr.Api
{
/// <summary>
/// Represents the <b>startup</b> class.
/// </summary>
public class Startup
{
/// <summary>
/// The configure services method called by the runtime; use this method to add services to the container.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
public void ConfigureServices(IServiceCollection services)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
// Add the core services.
services.AddSettings<HrSettings>()
.AddExecutionContext()
.AddJsonSerializer()
.AddReferenceDataOrchestrator<IReferenceDataProvider>()
.AddWebApi()
.AddJsonMergePatch()
.AddReferenceDataContentWebApi()
.AddRequestCache()
.AddValidationTextProvider()
.AddValidators<EmployeeManager>();
// Add the beef database services (scoped per request/connection).
services.AddDatabase(sp => new HrDb(() => new SqlConnection(sp.GetRequiredService<HrSettings>().DatabaseConnectionString), sp.GetRequiredService<ILogger<HrDb>>()));
// Add the beef entity framework services (scoped per request/connection).
services.AddDbContext<HrEfDbContext>();
services.AddEfDb<HrEfDb>();
// Add the generated reference data services.
services.AddGeneratedReferenceDataManagerServices()
.AddGeneratedReferenceDataDataSvcServices()
.AddGeneratedReferenceDataDataServices();
// Add the generated entity services.
services.AddGeneratedManagerServices()
.AddGeneratedDataSvcServices()
.AddGeneratedDataServices();
// Add event publishing services.
services.AddNullEventPublisher();
// Add transactional event outbox services.
services.AddScoped<IEventSender>(sp =>
{
var eoe = new EventOutboxEnqueue(sp.GetRequiredService<IDatabase>(), sp.GetRequiredService<ILogger<EventOutboxEnqueue>>());
//eoe.SetPrimaryEventSender(/* the primary sender instance; i.e. service bus */); // This is optional.
return eoe;
});
// Add entity mapping services using assembly probing.
services.AddMappers<HrSettings>();
// Add additional services.
services.AddControllers();
services.AddHealthChecks();
services.AddHttpClient();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My.Hr API", Version = "v1" });
var xmlName = $"{Assembly.GetEntryAssembly()!.GetName().Name}.xml";
var xmlFile = Path.Combine(AppContext.BaseDirectory, xmlName);
if (File.Exists(xmlFile))
options.IncludeXmlComments(xmlFile);
options.OperationFilter<AcceptsBodyOperationFilter>(); // Needed to support AcceptsBodyAttribute where body parameter not explicitly defined.
options.OperationFilter<PagingOperationFilter>(); // Needed to support PagingAttribute where PagingArgs parameter not explicitly defined.
});
}
/// <summary>
/// The configure method called by the runtime; use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
public void Configure(IApplicationBuilder app)
{
// Add exception handling to the pipeline.
app.UseWebApiExceptionHandler();
// Add Swagger as a JSON endpoint and to serve the swagger-ui to the pipeline.
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My.Hr"));
// Add execution context set up to the pipeline.
app.UseExecutionContext();
app.UseReferenceDataOrchestrator();
// Add health checks.
app.UseHealthChecks("/health");
// Use controllers.
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}