Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Windows Services in Visual Studio 2005 Standard Edition

RSS
Modified on Fri, Sep 30, 2011, 7:53 PM by Administrator Categorized as ┬ĚNet Framework

Overview

To create a Windows Service in Visual Studio, typical instructions start with creating a Windows Service project. However, this project type is not available in Visual Studio 2005 Standard Edition. This articles is a walkthrough of creating a Windows Service in Visual Studio 2005 Standard Edition.

Walkthrough

Creating the Project and Objects

  • Create a Console Application project.

  • Add a Windows Service item to the project.

Image

  • In the Properties window, specify the (Name) and ServiceName properties. Then click the Add installer link.

Image

  • Clicking the Add Installer link creates a Project Installer.cs file with two components: serviceProcessInstaller1 and serviceInstaller1.

  • For serviceProcessInstaller1, specify an Account property of LocalSystem.

Image

  • For serviceInstaller1, specify a StartType of Automatic and specify a user-friendly name for DisplayName.

Image

Coding the Service

  • In Program.cs, replace the Program class with the following code.

{copytext|ProgramCs}
class Program
{
    static void Main()
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun
            = new System.ServiceProcess.ServiceBase[] { new WeatherWidgetService() };

        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
} 

  • In WeatherWidgetService.cs, replace the WeatherWidgetService class with the following code.

{copytext|WeatherWidgetServiceCs}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace Weather_Widget_Service
{
    partial class WeatherWidgetService : ServiceBase
    {
        public WeatherWidgetService()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            WriteLog("Service started");
        }
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop 
            // your service.
            WriteLog("Service stopped");
        }
        private void WriteLog(string msg)
        {
            msg = "\n" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + ">" + msg;
            File.AppendAllText(@"C:\Data\WeatherService.log", msg);
        }
    }
} 

Deploying and Testing the Service

  • Build the service

  • Open a Visual Studio Command Window

  • Navigate to the folder containing the EXE (bin\Debug or bin\Release).

  • If the service is already installed from a previous installation, type the command installutil /u WeatherWidgetService.exe

  • Type the command installutil WeatherWidgetService.exe

  • Start the service via Control Panel > Administrative Tools > Services.

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.