Use topshelf to create a Windows Service
Winndows service is a program that can be started with the Windows operating system. It runs in the background and generally does not interact with users. It cannot be run by double-clicking, similar to the Unix daemon processes. It will not stop when the user logs out.
Windows Services consist of three parts:
- A service executable file;
- One Service Control Program (SCP );
- The Service Control Manager (SCM) is responsible for creating service key values under HKLM "system" CurrentControlSet "services. You can use SCP to control the startup, stop, and suspension of a service. SCP calls the service program through SCM.
Developing a Windows service is usually relatively simple. during development, we hope to run the service in the command line mode and want to have more control over the Windows service, there is a Windows service framework topshelf that can meet your requirements. Using this framework requires you to use an IOC container and use the common service locator interface in the framework, you can select your preferred IOC container based on your preferences.
For a basic introduction to topshelf, see topshelf In the Dru sellers introductory article. The following code creates a Windows Service:
Using system;
Using system. Collections. Generic;
Using system. IO;
Using system. Timers;
Using log4net. config;
Using Microsoft. Practices. servicelocation;
Using structuremap;
Using topshelf;
Using topshelf. configuration;
Internal class Program
{
Static void main (string [] ARGs)
{
Xmlconfigurator. configureandwatch (New fileinfo (". \ log4net. config "));
Irunconfiguration CFG = runnerconfigurator. New (x =>
{
X. afterstoppingthehost (H => {console. writeline ("afterstop called invoked, services are stopping ");});
X. setdescription ("sample topshelf host ");
X. setdisplayname ("stuff ");
X. setservicename ("stuff ");
X. configureserviceinisolation <towncrier> ("Tc", S =>
{
S. createservicelocator () =>
{
Objectfactory. initialize (I =>
{
I. forconcretetype <towncrier> (). Configure. withname ("Tc ");
});
Return new structuremapservicelocator ();
});
S. whenstarted (TC => TC. Start ());
S. whenstopped (TC => TC. Stop ());
});
X. runaslocalsystem ();
});
Runner. HOST (CFG, argS );
}
}
Here, structuremap is used as the IOC container, and a structuremapservicelocator is created to hide structuremap. The name of the created Windows service is stuff. You can use the corresponding method to start and stop the service. Run the. exe file directly on the console or debug the service.
Run the command to install and uninstall the Windows Service
Stuff.exe # Run in Console mode
Stuff.exe/install # Install the Windows Service
Stuff.exe/uninstall # uninstall Windows Services
By default, Windows Services can run only one instance. If you want to run multiple instances, you can add-Instance <Instance name>You can also read the configuration file during runtime to specify the Instance name. I prefer to use the latter method to set it. Add a configuration windowsserviceinstancename to the application configuration file:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
configuration
>
<
appSettings
file
=
"applicationSettings.config"
>
<
add
key
=
"WindowsServiceInstanceName"
value
=
"Stuff"
/>
</
appSettings
>
</
configuration
>
Then modify the above Code
Static void main (string [] ARGs)
{
VaR InstanceName = configurationmanager. receivettings ["windowsserviceinstancename"];
Xmlconfigurator. configureandwatch (New fileinfo (". \ log4net. config "));
Irunconfiguration CFG = runnerconfigurator. New (x =>
{
X. afterstoppingthehost (H => {console. writeline ("afterstop called invoked, services are stopping ");});
X. setdescription ("sample topshelf host ");
X. setdisplayname (InstanceName );
X. setservicename (InstanceName );
......
In this way, we can install multiple Windows service instances on the same machine. We recommend that you use this Windows service framework topshelf to simplify a lot of work and increase flexibility.