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

Allow run as a windows service #274

Merged
merged 8 commits into from Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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 \
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
@@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should check first if the system is Windows

{
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
@@ -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
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>