Directory Structure:
contents structure [-]
Create a Windows service
Configuration
Install Windows Service
Debug in Visual Studio
common problem
Recently wrote a program for TCP connection, because this communication protocol is different from HTTP protocol, so it can not be deployed on the website, so the Window service is used. Then the author introduces how to install and debug the Windows service in Visual Studio. The author's Visual studio version is 2012, and the window version is win7.
1. Create a Windows service
At this time, click the "Start" button, it will prompt us to start failed.
This is because the Windows service cannot be started like a normal window program, and then the service is installed.
2. Configuration
Right click on Service1.cs and select "View Designer",
Then in the blank space above, right-click and select "Add Installer",
At this time, Visual Studio will automatically generate a ProjectInstaller.cs file for us. Next, right-click ProjectInstaller.cs and select "View Designer", you can see the following interface,
You can see two controls, click and right-click the first control, open the property designer, set the Account to LocalSystem (local service),
Next, right-click the second control above, open the properties interface, set ServiceName, and set StratType to Automatic,
Next, in order to test the need, the author writes some test code:
public partial class Service1: ServiceBase
{
private readonly Timer _MyTimer;
public Service1 ()
{
InitializeComponent ();
_MyTimer = new Timer (10 * 1000); // Start once every 10 seconds
_MyTimer.Elapsed + = _MyTimerElapsed;
}
protected override void OnStart (string [] args)
{
_MyTimer.Start ();
}
protected override void OnStop ()
{
_MyTimer.Stop ();
}
internal void _MyTimerElapsed (object sender, ElapsedEventArgs e)
{
try
{
// Add a program to create a new text file to the C drive
try
{
string logFileName = @ "C: \\ log.txt"; // file path
FileInfo fileinfo = new FileInfo (logFileName);
using (FileStream fs = fileinfo.OpenWrite ())
{
StreamWriter sw = new StreamWriter (fs);
sw.WriteLine ("=====================================" = ";;
sw.Write ("Add date as:" + DateTime.Now.ToString () + "\ r \ n");
sw.WriteLine ("=====================================" = ";;
sw.Flush ();
sw.Close ();
}
}
catch (Exception ex)
{
// ex.ToString ();
}
}
catch (Exception ex)
{}
}
}
The above code, the author wrote in the Service1.cs class, which is a class file that is automatically created for us when Visual Sudio starts. Observing this class file, you can find that this class inherits ServiceBase. ServiceBase is the base class of the service class. That is to say, if you want to create a service, you must inherit this class. There are two methods in ServiceBase that are commonly used, namely OnStart () and OnStop (), as the name suggests, the OnStart method is called when the service is started, and the OnStop method is called when the service is stopped.
After the test code is written, we can click "Generate" to generate the solution. If you need to modify it later, you need to click to regenerate the solution:
After the solution is generated, the next step is to install the Windows service.
3. Install Windows Service
Find the exe file generated under the bin \ debug directory in the above project, where the author's location is D: \ Dev \ Test \ day20171218 \ MyWindowsService \ MyWindowsService \ bin \ Debug \ MyWindowsService.exe
Then open cmd and enter the directory of the corresponding version of .net. The author's version is 4.0 here. You can check the version information in the directory by C: \ Windows \ Microsoft.NET \ Framework. In general, the highest version is the current version.
Enter the corresponding version in CMD,
Then install through Installer.exe. When installing, you need to specify the location where the service exe file is generated, such as:
InstallUtil.exe D: \ Dev \ Test \ day20171218 \ MyWindowsService \ MyWindowsService \ bin \ Debug \ MyWindowsService.exe
If you want to uninstall, just add / u after InstallUtil.exe, for example:
InstallUtil.exe / u D: \ Dev \ Test \ day20171218 \ MyWindowsService \ MyWindowsService \ bin \ Debug \ MyWindowsService.exe
After the service is installed, you can start the service.
Press "Win + R" to open the running interface box, then enter services.msc, enter the service interface, find the corresponding service and start.
In the OnStart method above, we printed the log under the C drive. Next, open the log.txt under the C drive above,
You can see that the log has been successfully printed.
So far, a Window Server service program is completed. Next, the author introduces how to debug the Windows service program in Visual Studio.
4. Debug in Visual Studio
The debugging of Windows services is different from ordinary programs,
First set a breakpoint in the code and add the Debugger.Launch () method to the _MyTimerElapsed method above.
Here you can regenerate the solution, and then select "Debug" to attach to the process
If not, refresh it and come out.
Next, restart the service and you can debug normally.
5. FAQ
If there is a securiy Exception when installing the windows service, you should start the window as an administrator.
If System.ComponentModel.Win32Exception appears when installing the Windows service, it is because when setting serviceProcessInstaller1 in Visual Studio, Account is not set to LcoalSystem.
[Windows Service] Windows Service is installed and debugged in Visual Studio