From 2f50dd224f4ce199f24c947006d875c4d6dc4d78 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Sat, 22 Dec 2018 19:54:19 +0800 Subject: [PATCH] Allow run as a windows service (#274) --- ci/Dockerfile | 4 +- neo-cli/Services/ConsoleServiceBase.cs | 61 +++++++++++++++++++++++--- neo-cli/Services/ServiceProxy.cs | 24 ++++++++++ neo-cli/neo-cli.csproj | 3 +- 4 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 neo-cli/Services/ServiceProxy.cs diff --git a/ci/Dockerfile b/ci/Dockerfile index 30d9d9d19..98c92286c 100644 --- a/ci/Dockerfile +++ b/ci/Dockerfile @@ -1,4 +1,4 @@ -FROM microsoft/dotnet:2.0-sdk +FROM microsoft/dotnet:2.1-sdk # Install dependencies: RUN apt-get update && apt-get install -y \ @@ -25,6 +25,6 @@ WORKDIR /opt/neo-cli-github # Build the project RUN dotnet restore RUN dotnet publish -c Release -RUN mv bin/Release/netcoreapp2.0/publish /opt/neo-cli +RUN mv bin/Release/netcoreapp2.1/publish /opt/neo-cli WORKDIR /opt diff --git a/neo-cli/Services/ConsoleServiceBase.cs b/neo-cli/Services/ConsoleServiceBase.cs index f0ecd8c14..a2d019276 100644 --- a/neo-cli/Services/ConsoleServiceBase.cs +++ b/neo-cli/Services/ConsoleServiceBase.cs @@ -1,12 +1,16 @@ using System; +using System.Diagnostics; +using System.IO; using System.Reflection; using System.Security; +using System.ServiceProcess; using System.Text; namespace Neo.Services { public abstract class ConsoleServiceBase { + protected virtual string Depends => null; protected virtual string Prompt => "service"; public abstract string ServiceName { get; } @@ -102,17 +106,64 @@ public static SecureString ReadSecureString(string prompt) public void Run(string[] args) { - OnStart(args); - RunConsole(); - OnStop(); + if (Environment.UserInteractive) + { + if (args.Length > 0 && args[0] == "/install") + { + if (Environment.OSVersion.Platform != PlatformID.Win32NT) + { + Console.WriteLine("Only support for installing services on Windows."); + return; + } + string arguments = string.Format("create {0} start= auto binPath= \"{1}\"", ServiceName, Process.GetCurrentProcess().MainModule.FileName); + if (!string.IsNullOrEmpty(Depends)) + { + arguments += string.Format(" depend= {0}", Depends); + } + Process process = Process.Start(new ProcessStartInfo + { + Arguments = arguments, + FileName = Path.Combine(Environment.SystemDirectory, "sc.exe"), + RedirectStandardOutput = true, + UseShellExecute = false + }); + process.WaitForExit(); + Console.Write(process.StandardOutput.ReadToEnd()); + } + else if (args.Length > 0 && args[0] == "/uninstall") + { + if (Environment.OSVersion.Platform != PlatformID.Win32NT) + { + Console.WriteLine("Only support for installing services on Windows."); + return; + } + Process process = Process.Start(new ProcessStartInfo + { + Arguments = string.Format("delete {0}", ServiceName), + FileName = Path.Combine(Environment.SystemDirectory, "sc.exe"), + RedirectStandardOutput = true, + UseShellExecute = false + }); + process.WaitForExit(); + Console.Write(process.StandardOutput.ReadToEnd()); + } + else + { + OnStart(args); + RunConsole(); + OnStop(); + } + } + else + { + ServiceBase.Run(new ServiceProxy(this)); + } } private void RunConsole() { bool running = true; -#if NET461 Console.Title = ServiceName; -#endif Console.OutputEncoding = Encoding.Unicode; Console.ForegroundColor = ConsoleColor.DarkGreen; diff --git a/neo-cli/Services/ServiceProxy.cs b/neo-cli/Services/ServiceProxy.cs new file mode 100644 index 000000000..c323bfd53 --- /dev/null +++ b/neo-cli/Services/ServiceProxy.cs @@ -0,0 +1,24 @@ +using System.ServiceProcess; + +namespace Neo.Services +{ + internal class ServiceProxy : ServiceBase + { + private ConsoleServiceBase service; + + public ServiceProxy(ConsoleServiceBase service) + { + this.service = service; + } + + protected override void OnStart(string[] args) + { + service.OnStart(args); + } + + protected override void OnStop() + { + service.OnStop(); + } + } +} diff --git a/neo-cli/neo-cli.csproj b/neo-cli/neo-cli.csproj index 4d6f97286..dbfbb62ce 100644 --- a/neo-cli/neo-cli.csproj +++ b/neo-cli/neo-cli.csproj @@ -5,7 +5,7 @@ Neo.CLI 2.9.3 The Neo Project - netcoreapp2.0 + netcoreapp2.1 neo-cli Exe Neo.CLI @@ -29,6 +29,7 @@ +