forked from Tewr/BlazorWorker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoCExamplePage.razor
143 lines (128 loc) · 5.02 KB
/
IoCExamplePage.razor
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@using BlazorWorker.Demo.IoCExample
@using BlazorWorker.Extensions.JSRuntime
@inject IWorkerFactory workerFactory
<div class="row">
<div class="col-5 col-xs-12">
<h1>.NET Worker Thread Service with IoC</h1>
This page demonstrates IoC / dependency injection.
In this example, the class MyServiceStartup is a factory class for MyIocService.
The factory is implemented using ServiceCollection as an IoC Container.
<br /><br />
@foreach (var item in new string[] { "Persistant startup class", "Startup class disposed with service" })
{
<div>
<input type="radio" name="variant" id="@item" value="@item" @onchange="RadioSelection" checked=@(RadioValue.Equals(item,StringComparison.OrdinalIgnoreCase)) />
<label for="@item">@item</label>
</div>
}
<button disabled=@RunDisabled @onclick=OnClick class="btn btn-primary">Run test</button><br /><br />
<br />
<br />
<strong>Output:</strong>
<hr />
<pre>
@output
</pre>
</div>
<div class="col-7 col-xs-12">
<GithubSource RelativePath="Pages/IoCExamplePage.razor" />
</div>
</div>
@code {
string output = "";
IWorker worker;
IWorkerBackgroundService<MyServiceStartup> startupService;
string canDisposeWorker => worker == null ? null : "disabled";
string RunDisabled => Running ? "disabled" : null;
bool Running = false;
string RadioValue = "Persistant startup class";
void RadioSelection(ChangeEventArgs args)
{
RadioValue = args.Value.ToString();
}
public async Task OnClick(EventArgs _)
{
Running = true;
//output = "";
var rn = Environment.NewLine;
try
{
if (worker == null)
{
worker = await workerFactory.CreateAsync();
}
var sw = new System.Diagnostics.Stopwatch();
IWorkerBackgroundService<MyIocService> myIocService;
var persistedStartup = RadioValue == "Persistant startup class";
var serviceCollectionDependencies = new string[] {
"Microsoft.Extensions.DependencyInjection.Abstractions.dll"
#if NET5_0_OR_GREATER
,"System.Diagnostics.Tracing.dll"
#endif
#if NET6_0_OR_GREATER
,"Microsoft.Extensions.DependencyInjection.dll"
#endif
};
if (persistedStartup)
{
if (startupService == null)
{
output = $"{rn}{LogDate()} Creating background service...";
StateHasChanged();
sw.Start();
startupService = await worker.CreateBackgroundServiceAsync<MyServiceStartup>(wo =>
wo.AddConventionalAssemblyOfService()
.AddAssemblyOf<Microsoft.Extensions.DependencyInjection.ServiceCollection>()
.AddAssemblies(serviceCollectionDependencies)
.AddBlazorWorkerJsRuntime()
);
output += $"{rn}{LogDate()} Background service created in {sw.ElapsedMilliseconds}ms";
StateHasChanged();
}
output += $"{rn}{LogDate()} Resolving instance...";
myIocService = await startupService.CreateBackgroundServiceAsync(startup => startup.Resolve<MyIocService>());
}
else
{
output += $"{rn}{LogDate()} Resolving Startup & instance...";
myIocService = await worker.CreateBackgroundServiceUsingFactoryAsync<MyServiceStartup, MyIocService>(
startup => startup.Resolve<MyIocService>(), wo =>
wo.AddConventionalAssemblyOfService()
.AddAssemblyOf<Microsoft.Extensions.DependencyInjection.ServiceCollection>()
.AddAssemblies(serviceCollectionDependencies)
.AddBlazorWorkerJsRuntime()
);
}
await using (myIocService)
{
await myIocService.RegisterEventListenerAsync(nameof(MyIocService.FiveCalled),
(object s, int five) =>
{
output += $"{rn}{LogDate()} OnFiveCalled: {five}";
StateHasChanged();
});
output += $"{rn}{LogDate()} Calling Five()...";
output += $"{rn}{LogDate()} Five() = {await myIocService.RunAsync(s => s.Five())}";
}
StateHasChanged();
}
catch (Exception e)
{
output += $"{rn}Error = {e}";
}
finally
{
Running = false;
}
}
public async Task OnDisposeWorker()
{
await worker.DisposeAsync();
worker = null;
startupService = null;
}
private string LogDate()
{
return DateTime.Now.ToString("HH:mm:ss:fff");
}
}