How To Install Windows Service C# Using Installutil
In this article I will discuss about the windows service development and installation. The benefit of having a windows service is that we can put a timer in the service and do the operation on specified fourth dimension span. Using that timer the service will continue performing its operation. I will develop the service using the C# equally language.
This article is divided into three section every bit follow
- Developing the windows service
- Debugging the windows service.
- Installing/Uninstalling the windows service.
If you lot want to learn about the WCF service you can go to following links.
- WCF contracts
- Self hosted WCF service
Developing the Windows Service
Step ane:Open the visual studio. Click On Add together new project. Nether Windows yous will notice Windows Service projection template. If you don't find it search for the windows service project template as shown in the below figure. I have selected the Windows service C# projection template.
I have named the service as Logging Service. I will use this service to log some information in the text file at some time interval.
I will get the service project every bit shown in the effigy below. By default Service1.cs is added to the solution, I take renamed it equally LogService.cs as shown beneath.
Step 2:To install the service to the windows motorcar as service we need to add together equally installer. Double click on the LogSerivce.cs and nosotros should be able to run into the designer view. Right Click on the designer view and Click on the AddInstaller as shown in the figure below.
Step iii:In this step I volition change the Code of the LogService.cs as shown below.
public fractional grade LogService : ServiceBase { private Timer timer; private Stopwatch scout; public LogService() { InitializeComponent(); } public void OnDebug() { OnStart(goose egg); } protected override void OnStart(string[] args) { timer = new Timer(3000); watch = new Stopwatch(); timer.Commencement(); watch.Showtime(); timer.Elapsed += Timer_Elapsed; } individual void Timer_Elapsed(object sender, ElapsedEventArgs eastward) { LogWriter.LogInfo(string.Format("Message Logger since {0} seconds starting of service", watch.Elapsed)); } protected override void OnStop() { watch.Stop(); timer.Stop(); timer.Dispose(); } }
To demonstrate the functionality of the windows service I have accept written the code to log something.The functionality can be based on your own requirements. I take added the below form to the projection for Logging the information which is not thread safe.
public class LogWriter { private static string folderPath = AppDomain.CurrentDomain.BaseDirectory; public static void LogInfo(string messsage) { if (!File.Exists(folderPath + @"\\" + "Log" + ".txt")) { // Create a file to write to. using (StreamWriter sw = File.CreateText(folderPath + @"\\" + "Log" + ".txt")) { sw.WriteLine(DateTime.Now + ":" + messsage + Environs.NewLine); } } else { using (StreamWriter sw = File.AppendText(folderPath + @"\\" + "Log" + ".txt")) { sw.WriteLine(DateTime.Now + ":" + messsage + Environment.NewLine); } } } }
Debugging the Windows Service
If we debug the above service we will get the error "Cannot commencement service from the command line or debugger.." as shown in the figure beneath.
To debug the service nosotros have to change the lawmaking of programme.cs equally shown below
static void Main() { #if DEBUG LogService myService = new LogService(); myService.OnDebug(); Organisation.Threading.Thread.Sleep(System.Threading.Timeout.Space); #else ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new LogService() }; ServiceBase.Run(ServicesToRun);

0 Response to "How To Install Windows Service C# Using Installutil"
Post a Comment