forked from MrOkiDoki/BattleBit-Community-Server-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (51 loc) · 2.2 KB
/
Program.cs
File metadata and controls
61 lines (51 loc) · 2.2 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
using DatabaseExample;
using DatabaseExample.Models;
using DatabaseExample.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
/*
* This place is basically the entry point of the application, with a nicer syntax.
* Normally we would be in Main(), this is basically the same thing, to avoid writing boilerplate code.
*/
/*
* First we set up the IoC/DI system: https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection
* We now begin setting up and configuring our application class, which we start by creating a generic host builder for configuration
* https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host
*/
var builder = Host.CreateApplicationBuilder(args);
// We then register the services to the IoC container, allowing us to inject them into other classes / services.
/*
* The CreateApplicationBuilder method:
* Loads app configuration from:
*
* - appsettings.json.
* - appsettings.{Environment}.json.
* - Secret Manager when the app runs in the Development environment.
* - Environment variables.
* - Command-line arguments.
*
* We can now get the desired connection string for the database out of one of those.
*/
var dbConnectionString = builder.Configuration.GetConnectionString("defaultConnection");
// Add our DB connection, this is scoped service that can be injected.
builder.Services.AddDbContext<DatabaseContext>(options =>
{
options.UseLazyLoadingProxies();
options.UseMySql(dbConnectionString, ServerVersion.AutoDetect(dbConnectionString));
});
// Register the repositories, they will be disposed when the service scope ends.
builder.Services.AddScoped<PlayerRepository>();
builder.Services.AddScoped<BannedWeaponRepository>();
builder.Services.AddScoped<GameServerRepository>();
// Game server listener. This is our hosted service.
builder.Services.AddHostedService<ListenerService>();
var app = builder.Build();
// Auto apply any pending db migrations on startup.
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetService<DatabaseContext>();
context.Database.Migrate();
}
app.Run();