-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (72 loc) · 3.11 KB
/
Program.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
using CaseStudy;
using Microsoft.Data.SqlClient;
var builder = Host.CreateApplicationBuilder(args);
// Configure Runtime for Window Service
builder.Services.AddWindowsService(options => { options.ServiceName = ".NET AWS CaseStudy Omegapoint"; });
// Register Service Layer in DI container
builder.Services.AddSingleton<IDatabaseService>(provider =>
{
var logger = provider.GetRequiredService<ILogger<IDatabaseService>>();
var databaseOption = builder.Configuration
.GetSection(nameof(DatabaseOption))
.Get<DatabaseOption>();
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
sqlConnectionStringBuilder.DataSource = databaseOption.Host;
sqlConnectionStringBuilder.InitialCatalog = databaseOption.Schema;
sqlConnectionStringBuilder.UserID = databaseOption.Username;
sqlConnectionStringBuilder.Password = databaseOption.Password;
sqlConnectionStringBuilder.TrustServerCertificate = true;
sqlConnectionStringBuilder.CommandTimeout = 300;
sqlConnectionStringBuilder.ApplicationName = ".NET AWS CaseStudy Omegapoint";
sqlConnectionStringBuilder.ApplicationIntent = ApplicationIntent.ReadWrite;
return new DatabaseService(logger, sqlConnectionStringBuilder.ConnectionString);
});
// Add Workers to the Runtime
// Add Database Seeder
builder.Services.AddSingleton<IHostedService>(provider =>
{
var logger = provider.GetRequiredService<ILogger<DatabaseSeeder>>();
var db = provider.GetRequiredService<IDatabaseService>();
var workerOption = builder.Configuration
.GetSection(nameof(WorkerOption))
.Get<WorkerOption>();
var l = workerOption!.SqlTableNames
.ConvertAll(input => new SqlTable(input));
return new DatabaseSeeder(logger, db, l);
});
// Add Sweden
builder.Services.AddSingleton<IHostedService>(provider =>
{
var logger = provider.GetRequiredService<ILogger<Worker>>();
var db = provider.GetRequiredService<IDatabaseService>();
var workerOption = builder.Configuration
.GetSection(nameof(WorkerOption))
.Get<WorkerOption>();
var interval = workerOption.Interval;
var sqlTable = workerOption!.SqlTableNames
.ConvertAll(input => new SqlTable(input))
.Find(table => table.Name.EndsWith("SE"));
var awsOption = builder.Configuration
.GetSection(nameof(AwsOption))
.Get<AwsOption>();
return new Worker(logger, db, interval, sqlTable!, awsOption!);
});
// Add Finland
builder.Services.AddSingleton<IHostedService>(provider =>
{
var logger = provider.GetRequiredService<ILogger<Worker>>();
var db = provider.GetRequiredService<IDatabaseService>();
var workerOption = builder.Configuration
.GetSection(nameof(WorkerOption))
.Get<WorkerOption>();
var interval = workerOption.Interval;
var sqlTable = workerOption!.SqlTableNames
.ConvertAll(input => new SqlTable(input))
.Find(table => table.Name.EndsWith("FI"));
var awsOption = builder.Configuration
.GetSection(nameof(AwsOption))
.Get<AwsOption>();
return new Worker(logger, db, interval, sqlTable!, awsOption!);
});
var host = builder.Build();
host.Run();