Introduction
Window service is typically used to host WCF services or scheduled jobs. The following is a record of its usage.
Create
After you create a window service project, you can see the program and the Service1 class. Programme is the main entrance to the process, while Service1 is the main place of our logic implementation, two key methods are OnStart and OnStop, The logic used to implement the service start and end.
Installation
In the Service1 class design interface Right click, choose Add Installer, you can complete the installation program creation.
Nlog
Window Service as a daemon, what happened to the error is difficult to obtain information, so need to make a log record. Below is an example of output logging every 3 seconds.
1. Open the NuGet package management interface, enter the Nlog search, install the Nlog and Nlog Configuration, and then modify it in Nlog.config, as follows
<TargetXsi:type= "File"name= "F"FileName= "${basedir}/logs/${shortdate}.log"Layout= "${longdate} ${uppercase:${level}} ${message}" /> <TargetXsi:type= "Console"name= "C"Layout= "[${longdate}][${level:uppercase=true}][${logger}]${message}${exception}" /> </Targets> <rules <Loggername="*"MinLevel= "Debug"WriteTo= "F,c" /> </rules>
2. Use the timer and Nlog classes in Service1 to achieve output log records every 3 seconds, as follows
Public Partial classService1:servicebase {ReadOnlyLogger Logger =Logmanager.getcurrentclasslogger (); ReadOnlyTimer timer=NewTimer (); PublicService1 () {InitializeComponent (); } protected Override voidOnStart (string[] args) {Timer. AutoReset=true; Timer. Interval= the;//Unit millisecondsTimer. elapsed+=timer_elapsed; Timer. Start (); } protected Override voidOnStop () {timer. Stop (); } Private voidTimer_elapsed (Objectsender, Elapsedeventargs e) {logger. Info (DateTime.Now.ToShortTimeString ()); } //The following two methods are created for ease of debugging Public voidStart () {OnStart (NULL); } Public voidStop () {OnStop (); } }
Deployment
The following is the installation and uninstallation scripts, respectively, to save the bat file, placed in the root directory of the program can be.
@echo Installation Services Set SVC_FILE=%CD%\WINDOWSSERVICEDEMO.EXESC create Service1 binpath= "%svc_file%" displayname= "Service1" depend= tcpip start= autonet start service1@pause@exit@echo Uninstall service net stop SERVICE1SC Delete service1@pause@exit
Debugging
VS does not seem to provide a tool for window service debugging, testing can only be seen through actual deployment, but you can use the Topshell homestay service to achieve debugging purposes.
1. http://topshelf-project.com/ can be down to the latest class library
2. Create a new console program, reference the service above, Topshell,nlog, can be encoded, as follows
classProgram {Static voidMain (string[] args) {Hostfactory.run (x={x.usenlog (); X.service<Service1> (s ={s.constructusing (name=NewService1 ()); s.whenstarted (TC=TC. Start ()); s.whenstopped (TC=TC. Stop ()); }); X.setservicename ("Service1"); X.setdisplayname ("Service1"); X.setdescription ("log logging every 3 seconds"); X.runaslocalsystem (); X.startautomatically (); }); }
3. Run the console program to see
Summary
This article briefly describes the steps for window service from creation to debugging. For better advice, please feel free.
Resources
C # Writing Windows service (Windows Services Program)
Create a Windows service using Topshelf
Nlog article Series--a catalogue of articles and a brief introduction
"Window Service" two or three things about window service