Skip to content

Commit 0cb40b3

Browse files
authored
Fix hosting samples (#414)
1 parent 9c94f2e commit 0cb40b3

File tree

9 files changed

+34
-38
lines changed

9 files changed

+34
-38
lines changed

samples/Hosting/Logging/Program.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6-
using nanoFramework.Hosting;
7-
6+
using Microsoft.Extensions.Hosting;
87
using Microsoft.Extensions.Logging;
98

109
namespace nanoFramework.Logging
@@ -15,8 +14,8 @@ public static void Main()
1514
{
1615
IHost host = CreateHostBuilder().Build();
1716

18-
host.Start();
19-
host.Stop();
17+
host.StartAsync();
18+
host.StopAsync();
2019
}
2120

2221
public static IHostBuilder CreateHostBuilder() =>

samples/Hosting/Logging/Services/LoggingServices.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6-
using nanoFramework.Hosting;
7-
6+
using System.Threading;
7+
using Microsoft.Extensions.Hosting;
88
using Microsoft.Extensions.Logging;
99

1010
namespace nanoFramework.Logging
@@ -20,7 +20,7 @@ public LoggingService(ILogger logger, ILoggerFactory loggerFactory)
2020
ServiceLogger = loggerFactory.CreateLogger(nameof(LoggingService));
2121
}
2222

23-
public void Start()
23+
public void StartAsync(CancellationToken cancellationToken)
2424
{
2525
Logger.Log(LogLevel.Information, new EventId(10, "Start"), "Logging started", null);
2626

@@ -41,7 +41,7 @@ public void Start()
4141
ServiceLogger.LogCritical("Critical");
4242
}
4343

44-
public void Stop()
44+
public void StopAsync(CancellationToken cancellationToken)
4545
{
4646
Logger.Log(LogLevel.Information, new EventId(11, "Stop"), "Logging stopped", null);
4747
}

samples/Hosting/SensorQueue/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55

66
using Microsoft.Extensions.DependencyInjection;
7-
using nanoFramework.Hosting;
7+
using Microsoft.Extensions.Hosting;
88

99
namespace Hosting
1010
{

samples/Hosting/SensorQueue/Services/DisplayService.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using System;
77
using System.Threading;
88
using System.Diagnostics;
9-
10-
using nanoFramework.Hosting;
9+
using Microsoft.Extensions.Hosting;
1110

1211
namespace Hosting
1312
{
@@ -20,11 +19,11 @@ public DisplayService(BackgroundQueue queue)
2019
_queue = queue;
2120
}
2221

23-
protected override void ExecuteAsync()
22+
protected override void ExecuteAsync(CancellationToken stoppingToken)
2423
{
2524
Debug.WriteLine($"Service '{nameof(DisplayService)}' is now running in the background.");
2625

27-
while (!CancellationRequested)
26+
while (!stoppingToken.IsCancellationRequested)
2827
{
2928
try
3029
{

samples/Hosting/SensorQueue/Services/MonitorService.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
using System;
77
using System.Diagnostics;
8-
9-
using nanoFramework.Hosting;
8+
using System.Threading;
9+
using Microsoft.Extensions.Hosting;
1010

1111
namespace Hosting
1212
{
@@ -20,23 +20,23 @@ public MonitorService(BackgroundQueue queue)
2020
_queue = queue;
2121
}
2222

23-
public override void Start()
23+
public virtual void StartAsync(CancellationToken cancellationToken)
2424
{
2525
Debug.WriteLine($"Service '{nameof(MonitorService)}' is now running in the background.");
2626

27-
base.Start();
27+
base.StartAsync(cancellationToken);
2828
}
2929

30-
protected override void ExecuteAsync()
30+
protected override void ExecuteAsync(CancellationToken stoppingToken)
3131
{
3232
Debug.WriteLine($"Queue Depth: {_queue.QueueCount}");
3333
}
3434

35-
public override void Stop()
35+
public virtual void StopAsync(CancellationToken cancellationToken)
3636
{
3737
Debug.WriteLine($"Service '{nameof(MonitorService)}' is stopping.");
3838

39-
base.Stop();
39+
base.StopAsync(cancellationToken);
4040
}
4141
}
4242
}

samples/Hosting/SensorQueue/Services/SensorService.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using System;
77
using System.Threading;
88
using System.Diagnostics;
9-
10-
using nanoFramework.Hosting;
9+
using Microsoft.Extensions.Hosting;
1110

1211
namespace Hosting
1312
{
@@ -22,11 +21,11 @@ public SensorService(BackgroundQueue queue)
2221
_random = new Random();
2322
}
2423

25-
protected override void ExecuteAsync()
24+
protected override void ExecuteAsync(CancellationToken stoppingToken)
2625
{
2726
Debug.WriteLine($"Service '{nameof(SensorService)}' is now running in the background.");
2827

29-
while (!CancellationRequested)
28+
while (!stoppingToken.IsCancellationRequested)
3029
{
3130
try
3231
{

samples/Hosting/Simple/Program.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Device.Gpio;
99
using System.Diagnostics;
1010
using Microsoft.Extensions.DependencyInjection;
11-
using nanoFramework.Hosting;
11+
using Microsoft.Extensions.Hosting;
1212

1313
namespace Hosting
1414
{
@@ -26,9 +26,9 @@ public static void Main()
2626
IHost host = builder.Build();
2727

2828
// blink for 5 seconds and then stop and dispose of host
29-
host.Start();
29+
host.StartAsync();
3030
Thread.Sleep(5000);
31-
host.Stop();
31+
host.StopAsync();
3232
host.Dispose();
3333
}
3434
}
@@ -57,31 +57,31 @@ public LedHostedService(HardwareService hardware)
5757
_hardware = hardware;
5858
}
5959

60-
public override void Start()
60+
public override void StartAsync(CancellationToken cancellationToken)
6161
{
6262
Debug.WriteLine("LED Hosted Service running.");
6363

64-
base.Start();
64+
base.StartAsync(cancellationToken);
6565
}
6666

67-
protected override void ExecuteAsync()
67+
protected override void ExecuteAsync(CancellationToken cancellationToken)
6868
{
6969
var ledPin = 16;
7070

7171
GpioPin led = _hardware.GpioController.OpenPin(ledPin, PinMode.Output);
7272

73-
while (!CancellationRequested)
73+
while (!cancellationToken.IsCancellationRequested)
7474
{
7575
led.Toggle();
7676
Thread.Sleep(100);
7777
}
7878
}
7979

80-
public override void Stop()
80+
public override void StopAsync(CancellationToken cancellationToken)
8181
{
8282
Debug.WriteLine("LED Hosted Service stopped.");
8383

84-
base.Stop();
84+
base.StopAsync(cancellationToken);
8585
}
8686
}
8787
}

samples/Hosting/SlowBlink/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55

66
using Microsoft.Extensions.DependencyInjection;
7-
using nanoFramework.Hosting;
7+
using Microsoft.Extensions.Hosting;
88

99
namespace Hosting
1010
{

samples/Hosting/SlowBlink/Services/LedService.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
using System.Threading;
77
using System.Device.Gpio;
8-
9-
using nanoFramework.Hosting;
8+
using Microsoft.Extensions.Hosting;
109

1110
namespace Hosting
1211
{
@@ -19,13 +18,13 @@ public LedService(IHardwareService hardware)
1918
_hardware = hardware;
2019
}
2120

22-
protected override void ExecuteAsync()
21+
protected override void ExecuteAsync(CancellationToken cancellationToken)
2322
{
2423
var ledPin = 16;
2524

2625
GpioPin led = _hardware.GpioController.OpenPin(ledPin, PinMode.Output);
2726

28-
while (!CancellationRequested)
27+
while (!cancellationToken.IsCancellationRequested)
2928
{
3029
led.Toggle();
3130
Thread.Sleep(1000);

0 commit comments

Comments
 (0)