-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (54 loc) · 1.92 KB
/
Program.cs
File metadata and controls
59 lines (54 loc) · 1.92 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
using Microsoft.Extensions.DependencyInjection;
int ReadNumber() {
string? line = Console.ReadLine();
bool ret = int.TryParse(line, out int result);
if (ret)
return result;
return 0;
}
Console.Write("Select a scope of \"MyService\": [1] Transient [2] Scoped [default] Singleton\n>> ");
int index1 = ReadNumber();
Console.Write("Select a scope of \"MyMessage\": [1] Transient [2] Scoped [default] Singleton\n>> ");
int index2 = ReadNumber();
Console.Write("Create Scope in Demo? [1] Yes [default] No\n>> ");
int index3 = ReadNumber();
var serviceCollection = new ServiceCollection();
switch (index1) {
case 1:
serviceCollection.AddTransient<MyService>();
break;
case 2:
serviceCollection.AddScoped<MyService>();
break;
default:
serviceCollection.AddSingleton<MyService>();
break;
}
switch (index2) {
case 1:
serviceCollection.AddTransient<IMessage, MyMessage>();
break;
case 2:
serviceCollection.AddScoped<IMessage, MyMessage>();
break;
default:
serviceCollection.AddSingleton<IMessage, MyMessage>();
break;
}
var serviceProvider = serviceCollection.BuildServiceProvider();
switch (index3) {
case 1:
Console.WriteLine("=== Run Scope 1 ===");
var scope1 = serviceProvider.CreateScope();
scope1.ServiceProvider.GetRequiredService<MyService>().WriteMessage();
scope1.ServiceProvider.GetRequiredService<MyService>().WriteMessage();
Console.WriteLine("=== Run Scope 2 ===");
var scope2 = serviceProvider.CreateScope();
scope2.ServiceProvider.GetRequiredService<MyService>().WriteMessage();
scope2.ServiceProvider.GetRequiredService<MyService>().WriteMessage();
break;
default:
serviceProvider.GetRequiredService<MyService>().WriteMessage();
serviceProvider.GetRequiredService<MyService>().WriteMessage();
break;
}