From 3d10ad29071165689b8989c0a7ca1c74030372df Mon Sep 17 00:00:00 2001 From: Peter Kottas Date: Mon, 27 Nov 2017 21:43:12 +0100 Subject: [PATCH] Added built in accounts and pack script. Readme updated to reflect these changes --- README.md | 12 ++++---- .../HostConfiguration.cs | 5 +++- ...terKottas.DotNetCore.WindowsService.csproj | 6 ++-- ...terKottas.DotNetCore.WindowsService.nuspec | 2 +- .../ServiceRunner.cs | 28 +++++++++++++++++-- .../pack.bat | 1 + 6 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 Source/PeterKottas.DotNetCore.WindowsService/pack.bat diff --git a/README.md b/README.md index 312a326..96b86c3 100644 --- a/README.md +++ b/README.md @@ -117,11 +117,13 @@ Community, feel encouraged to add more templates if you find something missing/u 8. Run the service with **action:start** and it will start the service. 9. Run the service with **action:stop** and it will stop the service. 10. Run the service with **username:YOUR_USERNAME**, **password:YOUR_PASSWORD** and **action:install** which installs it for the given account. -11. Run the service with **description:YOUR_DESCRIPTION** and it setup description for the service. -11. Run the service with **displayName:YOUR_DISPLAY_NAME** and it setup Display name for the service. -12. Run the service with **name:YOUR_NAME** and it setup name for the service. -13. You can find the complete example in PeterKottas.DotNetCore.Example project. -14. Install the service using powershell: New-Service -Name $serviceName -BinaryPathName "$binPath action:run" -StartupType Automatic +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**. +12. Run the service with **description:YOUR_DESCRIPTION** and it setup description for the service. +13. Run the service with **displayName:YOUR_DISPLAY_NAME** and it setup Display name for the service. +14. Run the service with **name:YOUR_NAME** and it setup name for the service. +15. Run the service with **start-immediately:(true|false)** to start service immediately after install. Defaults to **true**. +16. You can find the complete example in PeterKottas.DotNetCore.Example project. +17. Install the service using powershell: New-Service -Name $serviceName -BinaryPathName "$binPath action:run" -StartupType Automatic ## Contributing diff --git a/Source/PeterKottas.DotNetCore.WindowsService/HostConfiguration.cs b/Source/PeterKottas.DotNetCore.WindowsService/HostConfiguration.cs index 302ee97..04d990c 100644 --- a/Source/PeterKottas.DotNetCore.WindowsService/HostConfiguration.cs +++ b/Source/PeterKottas.DotNetCore.WindowsService/HostConfiguration.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using PeterKottas.DotNetCore.WindowsService.Enums; using PeterKottas.DotNetCore.WindowsService.Interfaces; +using DasMulli.Win32.ServiceUtils; namespace PeterKottas.DotNetCore.WindowsService { @@ -30,7 +31,9 @@ public HostConfiguration() public string DisplayName { get; set; } - public bool StartImmediately { get; set; } + public bool StartImmediately { get; set; } = true; + + public Win32ServiceCredentials DefaultCred { get; set; } = Win32ServiceCredentials.LocalSystem; public SERVICE Service { get; set; } diff --git a/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.csproj b/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.csproj index 021dfb7..1fc62fc 100644 --- a/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.csproj +++ b/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.csproj @@ -2,9 +2,9 @@ netcoreapp2.0 - 2.0.2 - 2.0.2.0 - 2.0.2.0 + 2.0.3 + 2.0.3.0 + 2.0.3.0 diff --git a/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.nuspec b/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.nuspec index de0031d..6d5fecc 100644 --- a/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.nuspec +++ b/Source/PeterKottas.DotNetCore.WindowsService/PeterKottas.DotNetCore.WindowsService.nuspec @@ -3,7 +3,7 @@ PeterKottas.DotNetCore.WindowsService PeterKottas.DotNetCore.WindowsService - 2.0.2 + 2.0.3 Peter Kottas Easiest to use library that allows one to host .net core as windows services there is. .NET Core Windows service diff --git a/Source/PeterKottas.DotNetCore.WindowsService/ServiceRunner.cs b/Source/PeterKottas.DotNetCore.WindowsService/ServiceRunner.cs index 6dd24f2..018f11d 100644 --- a/Source/PeterKottas.DotNetCore.WindowsService/ServiceRunner.cs +++ b/Source/PeterKottas.DotNetCore.WindowsService/ServiceRunner.cs @@ -45,7 +45,31 @@ public static int Run(Action> runAction) }); config.AddParameter(new CmdArgParam() { - Key = "startimmediately", + Key = "built-in-account", + Description = "Password for the service account", + Value = val => + { + switch (val.ToLower()) + { + case "localsystem": + innerConfig.DefaultCred = Win32ServiceCredentials.LocalSystem; + break; + case "localservice": + innerConfig.DefaultCred = Win32ServiceCredentials.LocalService; + break; + case "networkservice": + innerConfig.DefaultCred = Win32ServiceCredentials.NetworkService; + break; + default: + innerConfig.DefaultCred = Win32ServiceCredentials.LocalSystem; + break; + } + + } + }); + config.AddParameter(new CmdArgParam() + { + Key = "start-immediately", Description = "Start the service immediately when installing.", Value = val => { @@ -201,7 +225,7 @@ private static string GetServiceCommand(List extraArguments) private static void Install(HostConfiguration config, ServiceController sc, int counter = 0) { - Win32ServiceCredentials cred = Win32ServiceCredentials.LocalSystem; + Win32ServiceCredentials cred = config.DefaultCred; if (!string.IsNullOrEmpty(config.Username)) { cred = new Win32ServiceCredentials(config.Username, config.Password); diff --git a/Source/PeterKottas.DotNetCore.WindowsService/pack.bat b/Source/PeterKottas.DotNetCore.WindowsService/pack.bat new file mode 100644 index 0000000..d160330 --- /dev/null +++ b/Source/PeterKottas.DotNetCore.WindowsService/pack.bat @@ -0,0 +1 @@ +nuget pack PeterKottas.DotNetCore.WindowsService.nuspec \ No newline at end of file