I want to developed windows service using visual studio 2019 using c# language .but when I select >> new project >> search I couldn't find windows service template . what I do next. how i create windows services using visual studio 2019. can we used console application as windows services??
Steps to create a window service.
Select Window Service. As shown below.
Add the installer. Double-click service1.cs and then click the right mouse button in the displayed interface to add the installer
Configure the properties of the installer. Configure the properties of serviceProcessInstaller and serviceInstaller.
Write the code of the service. OnStart: execute when the service starts, OnStop: execute when the service stops. Write logs when starting and stopping execution.
string filePath = "C:\\temp\\1.txt";
protected override void OnStart(string[] args)
{
StreamWriter writer = new StreamWriter(filePath, true);
writer.WriteLine(DateTime.Now.ToString("yy-MM-dd HH:mm:ss" + ":service start"));
writer.Flush();
writer.Close();
}
protected override void OnStop()
{
StreamWriter writer = new StreamWriter(filePath, true);
writer.WriteLine(DateTime.Now.ToString("yy-MM-dd HH:mm:ss" + ":service close"));
writer.Flush();
writer.Close();
}
Generate the .exe file. Because the direct opening of MyService.exe will report an error and the service cannot be started. It requires installutil to install