Woof.Service quick usage guide
To create self-installing Windows Service using Woof.Service
-
Start new
.NET Framework Console Application. -
Edit
Application/Assembly Information, fill every fied, make sureTitleandProductfields are not the same. -
The
Titlefield will be used as service display name, theProductas the service strong name,Companyas Windows log name. -
Get
Woof.ServiceInstall-Package Woof.Service -Version 2.0.0
-
Create new
ServiceInstaller.csfile in main directory with following content:using Woof.ServiceEx; public sealed class ServiceInstaller : ServiceInstallerEx { } public sealed class ServiceProcessInstaller : ServiceProcessInstallerEx { }
-
Create
Messagesdirectory. -
Create empty
Service.resxfile. -
Create main service class
Service.csextendingServiceBaseEx:using System; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using Woof.ServiceEx; namespace ServiceTest { class Service : ServiceBaseEx { public new static Service Instance => ServiceBaseEx.Instance as Service; public Service() : base(Messages.Service.ResourceManager) { } protected override void OnStart(string[] args) { base.OnStart(args); // here goes the code to perform on service start... } protected override void OnStop() { base.OnStop(); // here goes the clean up on service stop... } } }
-
Edit
Program.csfile like this:using Woof.ServiceEx; namespace ServiceTest { class Program { static int Main(string[] args) => new ServiceConsole<Service>(args).ReturnValue; } }
-
Compile...
-
Test with
-tswitch, install with-iswitch, uninstall with-uswitch. More help with-hswitch. -
Refer to
Woof.ServiceEx.ServiceBaseEx.Signal()andWoof.ServiceEx.ServiceBaseEx.WriteEvent()XML documentation to write to the service log correctly. -
Use
Messages\Service.resxresource file to define standard localized event messages like:Name Value Comment I1001 My dummy info event message W2001 My dummy warning event message E3001 My dummy error event message -
Use
ResXManagerextension to localize the messages. -
The event names must consist of severity character being one of
'I','W', or'E'and numeric event identifier. -
The message values can contain
{0}...{n}parameter placeholders in this exact format. -
The pre-configured event messages are available via
Signal()method. -
For all other events use
WriteEvent()method with0as event identifier. -
WARNING: Consider
CurrentDirectoryis set toC:\Windows\System32when refering to any files. -
To refer to a file in service
exedirectory use following:var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
-
Note the service started as
LocalSystemuser cannot use GUI, however user interaction is possible using inter-process communication available inWoof.Corepackage,Woof.SystemExnamespace.WPForWinFormsapplication can interact with the service.
Q&A
- Is there a easier or simpler way of doing this?
- Nope. Not for now but stay tuned, maybe a sample package / project template will be released soon.
- How safe and stable are those libraries?
- Tested for years on real production server environments.
- .NET Core...?
- Nope. Windows OS (desktop / server) is required.
Disclaimer
Installing a Windows Service on a server is a serious thing. Be warned you can do some serious damage to the system using this code. Get a testing environment (like your local PC) to test the service properly before deploying to production. CodeDog Ltd is not responsible for any damage caused using this library. You are.