Transferred from: http://blog.csdn.net/huwei2003/article/details/42168655
Create a Windows service using Topshelf
This is another way to create a Windows service, a foreigner article create a. NET Windows service in 5 steps with Topshelf (http://www.christophdebaene.com/blog/ 2011/03/16/create-a-net-windows-service-in-5-steps-with-topshelf/) uses the topshelf to create a Windows service using a 5-step detailed introduction. Topshelf is an open-source, cross-platform hosting service framework that supports Windows and mono and can build a convenient service host with just a few lines of code.
1, topshelf code hosted in http://topshelf-project.com/, you can download to the latest code here.
2. Use Visual Studio to create a console application that references the assembly TopShelf.dll Log4net.dll.
Find topshelf with NuGet to install Topshelf and topshelf.log4net
3, create a simple service class, which contains two methods start and stop, this service is just demo code, so we output a log every 5 seconds.
Using System;
Using System.Timers;
Using Log4net;
Namespace Samplewindowsservice
{
public class Sampleservice
{
Private Timer _timer = null;
ReadOnly ILog _log = Logmanager.getlogger (typeof (Sampleservice));
Public Sampleservice ()
{
Double interval = 5000;
_timer = new timer (interval);
_timer. Elapsed + = new Elapsedeventhandler (OnTick);
}
protected virtual void OnTick (object sender, Elapsedeventargs e)
{
_log. Debug ("Tick:" + DateTime.Now.ToLongTimeString ());
}
public void Start ()
{
_log. Info ("Sampleservice is Started");
_timer. AutoReset = true;
_timer. Enabled = true;
_timer. Start ();
}
public void Stop ()
{
_log. Info ("Sampleservice is Stopped");
_timer. AutoReset = false;
_timer. Enabled = false;
}
}
}
4, in the main method using Topshelf host our services, mainly to tell Topshelf how to set up the configuration of our services and start and stop the time of the method call.
Using System.IO;
Using Log4net. Config;
Using Topshelf;
Namespace Samplewindowsservice
{
Class Program
{
static void Main (string[] args)
{
Xmlconfigurator.configureandwatch (
New FileInfo (". \\log4net.config"));
To add Log4net.config to the project
var host = hostfactory.new (x =
{
X.enabledashboard (); If there is a problem, comment out the line
X.service<sampleservice> (s = =
{
S.setservicename ("Sampleservice");//If there is a problem, comment out the line
S.constructusing (name = new Sampleservice ());
s.whenstarted (TC =
{
Xmlconfigurator.configureandwatch (
New FileInfo (". \\log4net.config"));
Tc. Start ();
});
s.whenstopped (TC = TC). Stop ());
});
X.runaslocalsystem ();
X.setdescription ("Sampleservice Description");
X.setdisplayname ("Sampleservice");
X.setservicename ("Sampleservice");
});
Host. Run ();
}
}
}
4, configure log4net and run our services, services can be used as a console to run, which is very convenient when developing. The installation of the service is very convenient
SampleWindowsService.exe Install
After the installation is successful, it can be started through the service console, or it can be run through a command
SampleWindowsService.exe start
The uninstall method of the service is also very simple.
SampleWindowsService.exe Uninstall
Code download http://download.csdn.net/detail/huwei2003/8302099 [vs2013 nuget Reference Package]
Create a Windows service using Topshelf