Generic Windows service host - makes an ordinary Console Application hostable in the following scenarios:
- To be F5-debugged locally - on your developer machine
- To be installed as a Windows Service - on the servers in your basement
- To be executed as an Azure Web Job - in the cloud!!
Based on Topshelf. Exposes a drastically simplified API, where "services" are simply factories that return something IDisposable
.
Targets .NET Standard 2.0, so you must target either netcoreapp2.0 (or later), or net462 (or later) in your Console Application.
Create YourNewAwesomeWindowsService
as a Console Application project targeting AT LEAST .NET 4.6.2 or .NET Core App 2.0.
Include the NuGet package 📦
Install-Package Topper -ProjectName YourNewAwesomeWindowsService
and clean up your Program.cs
so it becomes nice like this: 🌻
namespace YourNewAwesomeWindowsService
{
class Program
{
static void Main()
{
}
}
}
and then you configure Topper by going
var configuration = new ServiceConfiguration()
.Add(.. function that returns an IDisposable ..)
.Add(.. another function that returns an IDisposable ..);
ServiceHost.Run(configuration);
in Main
, which could look like this:
namespace YourNewAwesomeWindowsService
{
class Program
{
static void Main()
{
var configuration = new ServiceConfiguration()
.Add(() => new MyNewAwesomeService());
ServiceHost.Run(configuration);
}
}
}
🐵 Easy!
Topper uses LibLog ⚡ to log things. If you want to use Serilog, you probably want to
Install-Package Serilog.Sinks.ColoredConsole -ProjectName YourNewAwesomeWindowsService
and configure the global 🌍 logger before starting your service:
namespace YourNewAwesomeWindowsService
{
class Program
{
static void Main()
{
Log.Logger = new LoggerConfiguration()
.WriteTo.ColoredConsole()
.CreateLogger();
var configuration = new ServiceConfiguration()
.Add(() => new MyNewAwesomeService());
ServiceHost.Run(configuration);
}
}
}
And that is how you use Topper.
Press F5 or CTRL+F5 in Visual Studio.
Run the .exe
Open an elevated command prompt, and run the .exe with the install
argument, like so:
C:\apps\YourApp> YourApp.exe install
and then some Windows Service Control 🚥 stuff will appear and tell you some details on how it was installed.
You can remove it again like this:
C:\apps\YourApp> YourApp.exe uninstall
Not exactly surprising. 👏
Just run it as you would any other Console Application as a Continuous Web Job.
Topper automatically monitors for the presence of the WEBJOBS_SHUTDOWN_FILE
, to be able to shut down gracefully and dispose your IDisposable
s. ♻️