Skip to content

Commit a96a0ee

Browse files
committed
Add test service, and removing the bool was a breaking change.
1 parent a22cfe0 commit a96a0ee

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

CodeCaster.WindowsServiceExtensions.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GitHub Workflows", "GitHub
1616
.github\workflows\Publish-Package.yml = .github\workflows\Publish-Package.yml
1717
EndProjectSection
1818
EndProject
19+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestServiceThatThrows", "test\TestServiceThatThrows\TestServiceThatThrows.csproj", "{A426812E-CB26-47F2-B914-17D87312C51F}"
20+
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2123
Debug|Any CPU = Debug|Any CPU
@@ -26,6 +28,10 @@ Global
2628
{CC230E5D-8600-4048-8F10-E84A80F77DDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
2729
{CC230E5D-8600-4048-8F10-E84A80F77DDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
2830
{CC230E5D-8600-4048-8F10-E84A80F77DDC}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{A426812E-CB26-47F2-B914-17D87312C51F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{A426812E-CB26-47F2-B914-17D87312C51F}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{A426812E-CB26-47F2-B914-17D87312C51F}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{A426812E-CB26-47F2-B914-17D87312C51F}.Release|Any CPU.Build.0 = Release|Any CPU
2935
EndGlobalSection
3036
GlobalSection(SolutionProperties) = preSolution
3137
HideSolutionNode = FALSE

src/CodeCaster.WindowsServiceExtensions/CodeCaster.WindowsServiceExtensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Company />
1515
<Copyright>CodeCaster</Copyright>
1616
<Description>Makes your .NET 5 BackgroundServices power-event-aware.</Description>
17+
<Version>2.0.0</Version>
1718
</PropertyGroup>
1819

1920
<ItemGroup>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using CodeCaster.WindowsServiceExtensions;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace TestServiceThatThrows
11+
{
12+
public class MyFaultyService : IHostedService
13+
{
14+
public Task StartAsync(CancellationToken cancellationToken)
15+
{
16+
throw new InvalidOperationException("This service is not supposed to start.");
17+
}
18+
19+
public Task StopAsync(CancellationToken cancellationToken)
20+
{
21+
return Task.CompletedTask;
22+
}
23+
}
24+
25+
public class MyFaultyBackgroundService : BackgroundService
26+
{
27+
protected override Task ExecuteAsync(CancellationToken stoppingToken)
28+
{
29+
// This works: gets logged in the event log, and prevents service startup.
30+
throw new InvalidOperationException("This service is not supposed to start.");
31+
}
32+
}
33+
34+
public static class Program
35+
{
36+
public static async Task Main(string[] args)
37+
{
38+
Thread.Sleep(5000);
39+
Debugger.Break();
40+
41+
await new HostBuilder()
42+
.ConfigureLogging(l => l.AddConsole())
43+
.ConfigureServices((s) =>
44+
{
45+
//throw new InvalidOperationException("Heh");
46+
//s.AddHostedService<MyFaultyService>();
47+
s.AddHostedService<MyFaultyBackgroundService>();
48+
})
49+
.UseWindowsService()
50+
//.UsePowerEventAwareWindowsService()
51+
.Build()
52+
.RunAsync();
53+
}
54+
}
55+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\CodeCaster.WindowsServiceExtensions\CodeCaster.WindowsServiceExtensions.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)