Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Allow run as a windows service (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Dec 22, 2018
1 parent 40d95a7 commit 2f50dd2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ci/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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
61 changes: 56 additions & 5 deletions neo-cli/Services/ConsoleServiceBase.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions neo-cli/Services/ServiceProxy.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 2 additions & 1 deletion neo-cli/neo-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyTitle>Neo.CLI</AssemblyTitle>
<Version>2.9.3</Version>
<Authors>The Neo Project</Authors>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>neo-cli</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Neo.CLI</PackageId>
Expand All @@ -29,6 +29,7 @@

<ItemGroup>
<PackageReference Include="Neo" Version="2.9.3" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="4.5.0" />
</ItemGroup>

</Project>

0 comments on commit 2f50dd2

Please sign in to comment.