Creating a Windows service with Topshelf briefly describes another way to create a Windows service, a foreigner article create a. NET Windows service in 5 steps with Topshelf uses Topshelf to create a Windows service by using the 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.
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"));
var host = hostfactory.new (x =
{
X.enabledashboard ();
X.service<sampleservice> (s = =
{
S.setservicename ("Sampleservice");
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
Topshelf Learning Cross-platform
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.
Official website: http://topshelf-project.com
Github:http://github.com/topshelf/topshelf
Here's how to start learning.
Create a project
1. Create a new console program
2. Adding references
We search directly for topshelf log4net
or NuGet console: Install-package topshelf.log4net
Under normal circumstances directly installed topshelf one on it. Log4net is just for the better convenience of logging.
3. Create a service
First create a class that inherits ServiceControl and then implements it.
public class Sampleservice:servicecontrol
{
LogWriter _log;
Timer _timer;
Public Sampleservice ()
{
_log = hostlogger.get<sampleservice> ();
_timer = new Timer (+) {AutoReset = true};
_timer. Elapsed + = (sender, EventArgs) = _log. Info (DateTime.Now);
}
public bool Start (Hostcontrol Hostcontrol)
{
_log. Info ("service start");
_timer. Start ();
return true;
}
public bool Stop (Hostcontrol Hostcontrol)
{
_log. Info ("service Stop");
_timer. Stop ();
return true;
}
}
LogWriter needs using Topshelf.logging;
4. Call
And then call in main
static void Main (string[] args)
{
Hostfactory.run (x =
{
X.uselog4net ("Log4net.config");
X.service<sampleservice> ();
X.setdescription ("topshelfsample description");
X.setdisplayname ("Topshelfsample display Name");
X.setservicename ("Topshelfsample service Name");
});
}
Such a simple service is done.
Log4net.config as follows:
<?xml version= "1.0" encoding= "Utf-8",
<log4net>
<appender name= "Rollingfile"
Type = "Log4net." Appender.fileappender,
<file value= "Service.log"/>
<appendtofile value= "true"/>
< Lockingmodel type= "log4net. Appender.fileappender+minimallock "/>
<layout type=" log4net. Layout.patternlayout "
<conversionpattern value="%-5p%d{hh:mm:ss}%message%newline "/>
</layout
</appender>
<appender name= "Coloredconsoleappender"
Type= "Log4net. Appender.coloredconsoleappender ">
<mapping>
<level value= "ERROR"/>
<forecolor value= "Red, highintensity"/>
</mapping>
<mapping>
<level value= "INFO"/>
<forecolor value= "White"/>
</mapping>
<mapping>
<level value= "DEBUG"/>
<forecolor value= "Cyan"/>
</mapping>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%message%newline"/>
</layout>
</appender>
<!--Set Root logger level-to-DEBUG and its-only appender to Console--
<root>
<level value= "DEBUG"/>
<appender-ref ref= "Rollingfile"/>
<appender-ref ref= "Coloredconsoleappender"/>
</root>
</log4net>
Installation Services
After the service has been written, we can install it directly, and the command to install it is very simple, directly TopshelfSample.exe install
has been successfully installed.
Start: TopshelfSample.exe start
Stop: TopshelfSample.exe stop
Uninstall: TopshelfSample.exe Uninstall
More commands can be TopshelfSample.exe help
Cross-platform
Topshelf is an open-source, cross-platform hosting service framework, but it only supports mono command-line execution and cannot use topshelf command-line start,stop control services.
Log4net Coloredconsoleappender does not support Linux, so remove the Coloredconsoleappender and keep the rollingfile.
Log4net.config
<?xml version= "1.0" encoding= "Utf-8"?>
<log4net>
<appender name= "Rollingfile"
Type= "Log4net. Appender.fileappender ">
<file value= "Service.log"/>
<appendtofile value= "true"/>
<lockingmodel type= "log4net. Appender.fileappender+minimallock "/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%-5p%d{hh:mm:ss}%message%newline"/>
</layout>
</appender>
<!--Set Root logger level-to-DEBUG and its-only appender to Console--
<root>
<level value= "DEBUG"/>
<appender-ref ref= "Rollingfile"/>
</root>
</log4net>
Let's look at the run under Linux.
First of all, we need to install mono.
Refer to: http://www.cnblogs.com/linezero/p/4774850.html
Test System version: CentOS 6.4 Mono 3.10.0
Upload the program to the Linux system for execution.
Mono TopshelfSample.exe
View logs for successful execution
Reference Documentation:
Https://topshelf.readthedocs.org/en/latest/configuration/quickstart.html
Create a Windows service using Topshelf