-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceBuilder.cs
More file actions
57 lines (40 loc) · 1.81 KB
/
ServiceBuilder.cs
File metadata and controls
57 lines (40 loc) · 1.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
using System.Text.Json.Serialization;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;
using TelegramBotService.Extensions;
using TelegramBotService.Services;
namespace TelegramBotService
{
internal class ServiceBuilder
{
private WebApplication? _app;
public void Start(string[] args)
{
// In Windows, network services set working directory to c:\windows\system32.
if (OperatingSystem.IsWindows())
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog as logging engine.
// Read configuration from appsettings.json, but always log to console.
builder.Host.UseSerilog((builderContext, loggerConf) => loggerConf
.ReadFrom.Configuration(builder.Configuration, sectionName: "Logger")
.WriteTo.Console(theme: SystemConsoleTheme.Colored)
.Enrich.WithProperty("Application", "CamMonitor.Bot"));
builder.Services.AddLocalization(config => config.ResourcesPath = "Resources");
builder.Services.AddTransient<LocalizationService>();
builder.Services.AddSingleton<ConfigurationService>();
builder.Services.AddTransient<CommandService>();
builder.Services.AddCommands();
builder.Services.AddBackgroundService<BotService>();
builder.Services.AddControllers().AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
_app = builder.Build();
_app.RunAsync();
}
public void Stop()
{
_app?.StopAsync().Wait();
_app?.DisposeAsync().AsTask().Wait();
}
}
}