-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
66 lines (59 loc) · 2.21 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
66 lines (59 loc) · 2.21 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
using Microsoft.Extensions.DependencyInjection;
using PostCodeSerialMonitor.ViewModels;
using PostCodeSerialMonitor.Services;
using System.Text.Json;
using System.Text.Json.Serialization;
using PostCodeSerialMonitor.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Console;
namespace PostCodeSerialMonitor;
public static class ServiceCollectionExtensions
{
const int SUPPORTED_CONFIG_FORMAT_VERSION = 2;
public static void AddCommonServices(this IServiceCollection collection)
{
// Configure logging
collection.AddLogging(builder =>
{
builder
.SetMinimumLevel(LogLevel.Debug)
.AddConsole(options =>
{
options.FormatterName = ConsoleFormatterNames.Simple;
});
});
// Build configuration
var configuration = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: false, reloadOnChange: true)
.Build();
// Configure options with validation
collection.AddOptions<AppConfiguration>()
.Bind(configuration.GetRequiredSection(nameof(AppConfiguration)))
.Validate(x => {
return x.FormatVersion == SUPPORTED_CONFIG_FORMAT_VERSION;
})
.ValidateOnStart();
// Register JSON serialization options
var jsonOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
WriteIndented = true,
Converters =
{
new JsonStringEnumConverter<MetaType>()
}
};
collection.AddSingleton(jsonOptions);
// Register services
collection.AddSingleton<ConfigurationService>();
collection.AddSingleton<SerialService>();
collection.AddSingleton<MetaUpdateService>();
collection.AddSingleton<MetaDefinitionService>();
collection.AddSingleton<SerialLineDecoder>();
collection.AddSingleton<GithubUpdateService>();
// Register ViewModels
collection.AddTransient<MainWindowViewModel>();
}
}