Skip to content

Commit d3ab916

Browse files
committed
Display name and events finished
1 parent 3d10ad2 commit d3ab916

6 files changed

Lines changed: 85 additions & 5 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ Community, feel encouraged to add more templates if you find something missing/u
9595
Console.WriteLine("Service {0} stopped", name);
9696
service.Stop();
9797
});
98+
99+
serviceConfig.OnInstall(service =>
100+
{
101+
Console.WriteLine("Service {0} installed", name);
102+
});
103+
104+
serviceConfig.OnUnInstall(service =>
105+
{
106+
Console.WriteLine("Service {0} uninstalled", name);
107+
});
108+
109+
serviceConfig.OnPause(service =>
110+
{
111+
Console.WriteLine("Service {0} paused", name);
112+
});
113+
114+
serviceConfig.OnContinue(service =>
115+
{
116+
Console.WriteLine("Service {0} continued", name);
117+
});
98118

99119
serviceConfig.OnError(e =>
100120
{
@@ -119,7 +139,7 @@ Community, feel encouraged to add more templates if you find something missing/u
119139
10. Run the service with **username:YOUR_USERNAME**, **password:YOUR_PASSWORD** and **action:install** which installs it for the given account.
120140
11. Run the service with **built-in-account:(NetworkService|LocalService|LocalSystem)** and **action:install** which installs it for the given built in account. Defaults to **LocalSystem**.
121141
12. Run the service with **description:YOUR_DESCRIPTION** and it setup description for the service.
122-
13. Run the service with **displayName:YOUR_DISPLAY_NAME** and it setup Display name for the service.
142+
13. Run the service with **display-name:YOUR_DISPLAY_NAME** and it setup Display name for the service.
123143
14. Run the service with **name:YOUR_NAME** and it setup name for the service.
124144
15. Run the service with **start-immediately:(true|false)** to start service immediately after install. Defaults to **true**.
125145
16. You can find the complete example in PeterKottas.DotNetCore.Example project.

Source/PeterKottas.DotNetCore.WindowsService/Configurators/Service/ServiceConfigurator.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,25 @@ public void OnError(Action<Exception> onError)
3131
{
3232
config.OnServiceError = onError;
3333
}
34+
35+
public void OnPause(Action<SERVICE> onPause)
36+
{
37+
config.OnServicePause = onPause;
38+
}
39+
40+
public void OnInstall(Action<SERVICE> onInstall)
41+
{
42+
config.OnServiceInstall = onInstall;
43+
}
44+
45+
public void OnUnInstall(Action<SERVICE> onUnInstall)
46+
{
47+
config.OnServiceUnInstall = onUnInstall;
48+
}
49+
50+
public void OnContinue(Action<SERVICE> onContinue)
51+
{
52+
config.OnServiceContinue = onContinue;
53+
}
3454
}
3555
}

Source/PeterKottas.DotNetCore.WindowsService/HostConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public HostConfiguration()
4343

4444
public Action<SERVICE> OnServiceStop { get; set; }
4545

46+
public Action<SERVICE> OnServiceInstall { get; set; }
47+
48+
public Action<SERVICE> OnServiceUnInstall { get; set; }
49+
50+
public Action<SERVICE> OnServicePause { get; set; }
51+
52+
public Action<SERVICE> OnServiceContinue { get; set; }
53+
4654
public Action<Exception> OnServiceError { get; set; }
4755

4856
public List<string> ExtraArguments { get; set; }

Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
55
<Version>2.0.3</Version>
6-
<AssemblyVersion>2.0.3.0</AssemblyVersion>
7-
<FileVersion>2.0.3.0</FileVersion>
6+
<AssemblyVersion>2.0.4.0</AssemblyVersion>
7+
<FileVersion>2.0.4.0</FileVersion>
88
</PropertyGroup>
99
<ItemGroup>
1010
<PackageReference Include="DasMulli.Win32.ServiceUtils" Version="1.0.1" />

Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>PeterKottas.DotNetCore.WindowsService</id>
55
<title>PeterKottas.DotNetCore.WindowsService</title>
6-
<version>2.0.3</version>
6+
<version>2.0.4</version>
77
<authors>Peter Kottas</authors>
88
<description>Easiest to use library that allows one to host .net core as windows services there is.</description>
99
<summary>.NET Core Windows service</summary>

Source/PeterKottas.DotNetCore.WindowsService/ServiceRunner.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static int Run(Action<HostConfigurator<SERVICE>> runAction)
9999
});
100100
config.AddParameter(new CmdArgParam()
101101
{
102-
Key = "displayName",
102+
Key = "display-name",
103103
Description = "Service display name",
104104
Value = val =>
105105
{
@@ -294,6 +294,7 @@ private static void Uninstall(HostConfiguration<SERVICE> config, ServiceControll
294294
}
295295
new Win32ServiceManager().DeleteService(config.Name);
296296
Console.WriteLine($@"Successfully unregistered service ""{config.Name}"" (""{config.Description}"")");
297+
config.OnServiceUnInstall?.Invoke(config.Service);
297298
}
298299
catch (Exception e)
299300
{
@@ -312,6 +313,37 @@ private static void StopService(HostConfiguration<SERVICE> config, ServiceContro
312313
sc.Stop();
313314
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMilliseconds(1000));
314315
Console.WriteLine($@"Successfully stopped service ""{config.Name}"" (""{config.Description}"")");
316+
config.OnServiceStop?.Invoke(config.Service);
317+
}
318+
else
319+
{
320+
Console.WriteLine($@"Service ""{config.Name}"" (""{config.Description}"") is already stopped or stop is pending.");
321+
}
322+
}
323+
324+
private static void PauseService(HostConfiguration<SERVICE> config, ServiceController sc)
325+
{
326+
if (!(sc.Status == ServiceControllerStatus.Paused | sc.Status == ServiceControllerStatus.PausePending))
327+
{
328+
sc.Pause();
329+
sc.WaitForStatus(ServiceControllerStatus.Paused, TimeSpan.FromMilliseconds(1000));
330+
Console.WriteLine($@"Successfully paused service ""{config.Name}"" (""{config.Description}"")");
331+
config.OnServicePause?.Invoke(config.Service);
332+
}
333+
else
334+
{
335+
Console.WriteLine($@"Service ""{config.Name}"" (""{config.Description}"") is already paused or stop is pending.");
336+
}
337+
}
338+
339+
private static void ContinueService(HostConfiguration<SERVICE> config, ServiceController sc)
340+
{
341+
if (!(sc.Status == ServiceControllerStatus.Running | sc.Status == ServiceControllerStatus.ContinuePending))
342+
{
343+
sc.Continue();
344+
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(1000));
345+
Console.WriteLine($@"Successfully stopped service ""{config.Name}"" (""{config.Description}"")");
346+
config.OnServiceContinue?.Invoke(config.Service);
315347
}
316348
else
317349
{

0 commit comments

Comments
 (0)