Create a Windows service Z using topshelf 5 steps

Source: Internet
Author: User
Tags log4net

Create a Windows service using Topshelf A brief description of another way to create a Windows service, a foreigner article create A. NET Windows service in 5 steps with Topshelf Use Topshelf to create a Windows service with a detailed introduction to the 5 steps. 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
{
PublicClass 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;
}
}
} /span> 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
{
Staticvoid Main (string[] args)
{
Xmlconfigurator.configureandwatch (
Span class= "KWRD" >new FileInfo (
var host = hostfactory.new (x =>
{
X.enabledashboard ();
X.service<sampleservice> (s =>
{
S.setservicename ( S.constructusing (name = new Sampleservice ());
s.whenstarted (tc =>
{
Xmlconfigurator.configureandwatch (
new FileInfo ( 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 convenientSampleWindowsService.exe Installafter the installation is successful, it can be started through the service console, or it can be run through a commandSampleWindowsService.exe StartThe Uninstall method of the service is also very simple.SampleWindowsService.exe UninstallRelated article: Create a Windows service using Topshelf

A WCF calculator in a Windows service with Topshelf

WCF service with Topshelf using as a host, log4net as logging tool, and HTML as output

Create a Windows service Z using topshelf 5 steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.