SoFunction
Updated on 2025-04-13

How to create Windows services with .NET page 2/2


Install Windows Services

Windows services are different from ordinary Windows applications. It is impossible to simply start the Windows service by running an EXE. Installing a Windows service should be done by using the .NET Framework provided, or through a file deployment project such as a Microsoft Installer (MSI).


Add a service installer

Creating a Windows service and using the InstallUtil program to install this service is not enough. You must also add a service installer to your Windows service, so that InstallUtil or any other installer knows what configuration settings you are using.

1. Switch this service program to the design view
2. Right-click on the design view and select "Add Installer"
3. Switch to the design view of the ProjectInstaller that has just been added
4. Set the properties of the serviceInstaller1 component:
    1) ServiceName = My Sample Service
    2) StartType = Automatic
5. Set the properties of the serviceProcessInstaller1 component
    1) Account = LocalSystem
6. Generate a solution

After completing the above steps, the following source code will be automatically generated by Visual Studio, which is included in this source file.

using System;
using ;
using ;
using ;

namespace
{
  /// <summary>
  /// Summary description for ProjectInstaller.
  /// </summary>
  [RunInstaller(true)]
  public class ProjectInstaller :

  {
   private
serviceProcessInstaller1;
   private serviceInstaller1;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private components = null;

   public ProjectInstaller()
   {
     // This call is required by the Designer.
     InitializeComponent();

     // TODO: Add any initialization after the InitComponent call
   }

   #region Component Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.serviceProcessInstaller1 = new
();
     this.serviceInstaller1 = new
();
     //
     // serviceProcessInstaller1
     //
     this. =
;
     this. = null;
     this. = null;
     //
     // serviceInstaller1
     //
     this. = "My Sample Service";
     this. =
;
     //
     // ProjectInstaller
     //
     (new
[]
{this.serviceProcessInstaller1, this.serviceInstaller1});
}
   #endregion
  }
}


Install Windows Services with InstallUtil

Now that this service has been generated, you need to install it before it can be used. The following operations will guide you to install your new service.

1. Open Visual Studio .NET command prompt
2. Change the path to the bin\Debug folder location where your project is located (if you compile in Release mode, it will be in the bin\Release folder)
3. Execute the command "" to register this service to create a suitable registration item.
4. Right-click "My Computer" on the desktop, select "Manage" to click on the computer management console
5. In the "Services" section of "Services and Applications", you can find that your Windows service is already included in the service list.
6. Right-click on your service and select Start to start your service.

Every time you need to modify the Windows service, this will require you to uninstall and reinstall the service. However, it is a good habit to make sure that the service management console is closed before uninstalling this service. If this is not done, you may have trouble uninstalling and reinstalling Windows services. If you only uninstall the service, you can execute the InstallUtil command in the phase to log out of the service, but you need to add a /u command switch afterwards.


Debugging Windows Services

From another perspective, debugging Windows services is definitely different from an ordinary application. More steps to debug Windows services. Services cannot be debugged as you do with ordinary applications simply executed in a development environment. The service must be installed and started first, which we have done in the previous section. To facilitate tracking of debugging code, once the service is started, you need to attach the running process using Visual Studio. Remember, any modifications made to your Windows service must be uninstalled and reinstalled.


Attach a running Windows service

To debug the program, some additional instructions for Windows services are attached. These operations assume that you have installed this Windows service and it is running.

1. Load this project with Visual Studio
2. Click the "Debug" menu
3. Click on the "Process" menu
4. Ensure that the display system process is selected
5. In the Available Process list, locate the process on your executable file name and click to select it
6. Click the Attached Button
7. Click OK
8. Click Close
9. Set a breakpoint in the timer1_Elapsed method and wait for it to execute


Summarize

Now you should have a rough idea of ​​what Windows services are and how to create, install and debug them. You can study the functions of Windows services yourself. These features include the ability to pause and resume (OnContinue). The ability to pause and restore is not enabled by default and must be set through the Windows Service Properties.


About the Author
Mark Strawmyer, MCSD, MCSE (NT4/W2K), MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@

Previous page12Read the full text