Alex Rivera | Logout

Multiple Windows Services in One exe

Asked 2009-05-03T05:55:55.310
28

I am trying to build several Windows services to do different things. For instance, I need Windows services that will:

  1. Send a daily report via email
  2. Periodically cleanup some archived info every 30 minutes
  3. etc.

The tasks I need the windows services to do are distinct so I don't really like the idea of having them all in one service.

What I've got so far is a project in Visual Studio 2008. I've created a windows service, I've set up a timer on the OnStart event (it just writes to a text file every 5 seconds for testing purposes). I then added an Installer to the project and when I run InstallUtil.exe, everything works fine.

The problem comes in when I add a second windows service to the same project. I set up the OnStart code again, with the same logging info (slightly different so I can tell which service is writing to the log). With the second windows service, I changed the Main event in Program.cs from:

    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun = new ServiceBase[] 
        { 
            new Service1()
        };

        ServiceBase.Run(ServicesToRun);
    }

to:

    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun = new ServiceBase[] 
        { 
            new Service1(),
            new Service2()
        };

        ServiceBase.Run(ServicesToRun);
    }

At this point, there are no compile time errors, but the Service2 service never does anything...the logging task never fires.

I narrowed it down to the fact that the second service has no "Installer" associated with it. I then tried to add an Installer the way I did with the first service (i.e., right click on the service designer, and click "Add Installer"). Now, when I go to the ProjectInstaller.cs file, there is another serviceInstaller there (serviceInstaller2).

Now when I build the p

Edit
Report

1 Answer

0

Installing 2 distinct and unrelated services in one executable has a funny smell to me. While it's possible to get it to work, it doesn't make it right.

From what it sounds like, you need to perform some different tasks at various intervals and your tasks may or may not be related. Have you looked into Quartz.NET? It sounds like a good fit for your needs.

http://quartznet.sourceforge.net/features.html

answered 2009-11-20T01:07:15.877

Your Answer