views:

51

answers:

1

Hello,

I have problem installing simple Windows Service, my code looks like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace Tools.FileMonitorService
{
    public class Service : System.ServiceProcess.ServiceBase
    {
        private System.ComponentModel.Container components = null;

        public Service()
        {
            InitializeComponent();
        }

        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = "Tools.FileMonitorService";  
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        protected override void OnStart(string[] args)
        {
        while (true)
        {
        Debug.Write("Ping");
        Thread.Sleep(10000);
               }            
        }

        protected override void OnStop()
        {
            Debug.Write("Stopping");
        }
    }
}

When I run following commnad:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i Tools.FileMonitorService.exe

There are no errors and everything seams fine, but when I look at Windows Services list, I can not see my service.

I'm doing something wrong?

I've tried doing it on Windows 7 and Windows 2008 Server

Thank You very much!

+2  A: 

There is no sign of a ServiceInstaller in your code snippet, nor an install method that is marked with the [RunInstaller(true)] attribute. Which would explain why it doesn't work.

There's a good example in the MSDN Library article for the ServiceInstaller class.

Hans Passant
Indeed I did not have ServiceInstaller classThank You
Daniil Harik