回答編集履歴

2

名前空間入れ忘れてました

2021/01/04 21:14

投稿

hskwakr
hskwakr

スコア9

test CHANGED
@@ -14,64 +14,68 @@
14
14
 
15
15
 
16
16
 
17
- public class Program
17
+ namespace ConsoleAppDependencyInjection
18
18
 
19
19
  {
20
20
 
21
- public static Task Main(string[] args)
21
+ public class Program
22
22
 
23
23
  {
24
24
 
25
+ public static Task Main(string[] args)
26
+
27
+ {
28
+
25
- IHost host = CreateHostBuilder(args).Build();
29
+ IHost host = CreateHostBuilder(args).Build();
26
30
 
27
31
 
28
32
 
29
- CreateScope(host.Services);
33
+ CreateScope(host.Services);
30
34
 
31
35
 
32
36
 
33
- return host.RunAsync();
37
+ return host.RunAsync();
34
38
 
35
- }
39
+ }
36
40
 
37
41
 
38
42
 
39
- private static IHostBuilder CreateHostBuilder(string[] args)
43
+ private static IHostBuilder CreateHostBuilder(string[] args)
40
44
 
41
- {
45
+ {
42
46
 
43
- return Host.CreateDefaultBuilder(args)
47
+ return Host.CreateDefaultBuilder(args)
44
48
 
45
- .ConfigureServices((_, services) =>
49
+ .ConfigureServices((_, services) =>
46
50
 
47
- services.AddSingleton<IOrderRepository, SqlOrderRepository>()
51
+ services.AddSingleton<IOrderRepository, SqlOrderRepository>()
48
52
 
49
- .AddSingleton<ILogger, Logger>()
53
+ .AddSingleton<ILogger, Logger>()
50
54
 
51
- .AddSingleton<CancelOrderHandler>());
55
+ .AddSingleton<CancelOrderHandler>());
52
56
 
53
- }
57
+ }
54
58
 
55
59
 
56
60
 
57
- private static void CreateScope(IServiceProvider services)
61
+ private static void CreateScope(IServiceProvider services)
58
62
 
59
- {
63
+ {
60
64
 
61
- using IServiceScope serviceScope = services.CreateScope();
65
+ using IServiceScope serviceScope = services.CreateScope();
62
66
 
63
- IServiceProvider provider = serviceScope.ServiceProvider;
67
+ IServiceProvider provider = serviceScope.ServiceProvider;
64
68
 
65
69
 
66
70
 
67
- var cancelHandler = provider.GetRequiredService<CancelOrderHandler>();
71
+ var cancelHandler = provider.GetRequiredService<CancelOrderHandler>();
68
72
 
69
- cancelHandler.Handle();
73
+ cancelHandler.Handle();
74
+
75
+ }
70
76
 
71
77
  }
72
78
 
73
79
  }
74
80
 
75
-
76
-
77
81
  ```

1

using入れ忘れてました

2021/01/04 21:14

投稿

hskwakr
hskwakr

スコア9

test CHANGED
@@ -3,6 +3,16 @@
3
3
 
4
4
 
5
5
  ```Csharp
6
+
7
+ using System;
8
+
9
+ using System.Threading.Tasks;
10
+
11
+ using Microsoft.Extensions.DependencyInjection;
12
+
13
+ using Microsoft.Extensions.Hosting;
14
+
15
+
6
16
 
7
17
  public class Program
8
18