-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/PeterKottas/DotNetCore.Wi…
- Loading branch information
Showing
8 changed files
with
225 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
root = true | ||
|
||
[*.cs] | ||
indent_style = space | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
Source/PeterKottas.DotNetCore.WindowsService.Example/ExampleServiceTimer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 1 addition & 5 deletions
6
Source/PeterKottas.DotNetCore.WindowsService.Example/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
Source/PeterKottas.DotNetCore.WindowsService/ConsoleServiceHost.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using PeterKottas.DotNetCore.WindowsService.Interfaces; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace PeterKottas.DotNetCore.WindowsService | ||
{ | ||
/// <summary> | ||
/// Copy of Topshelf ConsoleRunHost | ||
/// https://github.com/Topshelf/Topshelf/blob/develop/src/Topshelf/Hosts/ConsoleRunHost.cs | ||
/// </summary> | ||
class ConsoleServiceHost<SERVICE> | ||
where SERVICE : IMicroService | ||
{ | ||
private InnerService _consoleService = null; | ||
private HostConfiguration<SERVICE> _innerConfig = null; | ||
private ExitCode _exitCode = 0; | ||
private ManualResetEvent _exit = null; | ||
private volatile bool _hasCancelled = false; | ||
|
||
public ConsoleServiceHost(InnerService consoleService, HostConfiguration<SERVICE> innerConfig) | ||
{ | ||
_consoleService = consoleService | ||
?? throw new ArgumentNullException(nameof(consoleService)); | ||
|
||
_innerConfig = innerConfig | ||
?? throw new ArgumentNullException(nameof(innerConfig)); | ||
} | ||
|
||
internal ExitCode Run() | ||
{ | ||
AppDomain.CurrentDomain.UnhandledException += CatchUnhandledException; | ||
|
||
bool started = false; | ||
try | ||
{ | ||
Console.WriteLine("Starting up as a console service host"); | ||
|
||
_exit = new ManualResetEvent(false); | ||
_exitCode = ExitCode.Ok; | ||
|
||
Console.Title = _consoleService.ServiceName; | ||
Console.CancelKeyPress += HandleCancelKeyPress; | ||
|
||
_consoleService.Start(_innerConfig.ExtraArguments.ToArray(), () => Console.WriteLine("Stopping console service host")); | ||
started = true; | ||
|
||
Console.WriteLine("The {0} service is now running, press Control+C to exit.", _consoleService.ServiceName); | ||
|
||
_exit.WaitOne(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("An exception occurred", ex); | ||
|
||
return ExitCode.AbnormalExit; | ||
} | ||
finally | ||
{ | ||
if (started) | ||
StopService(); | ||
|
||
_exit.Close(); | ||
(_exit as IDisposable).Dispose(); | ||
} | ||
|
||
return _exitCode; | ||
} | ||
|
||
internal void StopService() | ||
{ | ||
try | ||
{ | ||
if (_hasCancelled) | ||
return; | ||
|
||
Console.WriteLine("Stopping the {0} service", _consoleService.ServiceName); | ||
|
||
Task stopTask = Task.Run(() => _consoleService.Stop()); | ||
if (!stopTask.Wait(TimeSpan.FromMilliseconds(150))) | ||
throw new Exception("The service failed to stop (returned false)."); | ||
|
||
_exitCode = ExitCode.Ok; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("The service did not shut down gracefully: {0}", ex.ToString()); | ||
_exitCode = ExitCode.AbnormalExit; | ||
} | ||
finally | ||
{ | ||
Console.WriteLine("The {0} service has stopped.", _consoleService.ServiceName); | ||
_exitCode = ExitCode.Ok; | ||
} | ||
} | ||
|
||
private void HandleCancelKeyPress(object sender, ConsoleCancelEventArgs e) | ||
{ | ||
if (e.SpecialKey == ConsoleSpecialKey.ControlBreak) | ||
{ | ||
Console.WriteLine("Control+Break detected, terminating service (not cleanly, use Control+C to exit cleanly)"); | ||
return; | ||
} | ||
|
||
e.Cancel = true; | ||
|
||
if (_hasCancelled) | ||
return; | ||
|
||
Console.WriteLine("Control+C detected, attempting to stop service."); | ||
Task stopTask = Task.Run(() => _consoleService.Stop()); | ||
if (stopTask.Wait(TimeSpan.FromMilliseconds(150))) | ||
{ | ||
_hasCancelled = true; | ||
_exit.Set(); | ||
} | ||
else | ||
{ | ||
_hasCancelled = false; | ||
Console.WriteLine("The service is not in a state where it can be stopped."); | ||
} | ||
} | ||
|
||
private void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e) | ||
{ | ||
Console.WriteLine("The service threw an unhandled exception: {0}", e.ToString()); | ||
|
||
if (!e.IsTerminating) | ||
return; | ||
|
||
_exitCode = ExitCode.UnhandledServiceException; | ||
_exit.Set(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace PeterKottas.DotNetCore.WindowsService | ||
{ | ||
/// <summary> | ||
/// Copy of: https://github.com/Topshelf/Topshelf/blob/develop/src/Topshelf/TopshelfExitCode.cs | ||
/// </summary> | ||
enum ExitCode | ||
{ | ||
Ok = 0, | ||
AbnormalExit = 1, | ||
SudoRequired = 2, | ||
ServiceAlreadyInstalled = 3, | ||
ServiceNotInstalled = 4, | ||
StartServiceFailed = 5, | ||
StopServiceFailed = 6, | ||
ServiceAlreadyRunning = 7, | ||
UnhandledServiceException = 8, | ||
ServiceNotRunning = 9, | ||
SendCommandFailed = 10, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters