R – Why can’t I start more than X number of windows service developed in .Net on one machine

netwindows-services

I have stumbled upon a situation where I cannot start more than a certain number of .Net windows services. Our software system is as set of 8-9 windows services and I want to create 5 logical environments on one machine (i.e about 40 services). To reproduce the problem and to prove that it is not an application issue I created a simple windows service.

I am compiling it to TestService.exe and then creating 30 copies of it. From TestService01.exe to TestService30.exe.
I am installing the services using SC as shown below

SC create "Test Service Copy 01" start= demand binpath= "C:\Temp\TestService\TestService01.exe" obj= "mydomain\myservices" password= "myservicepwd" displayname= "Test Service Copy 01"

Now I can start somewhere between 15 and 25 of those services on a windows server 2003 machine. After that I can't start another service. It fails with the error messagebox

TestService18.exe - Application Error 
--------------------------- 
The exception unknown software exception (0xc06d007e) occurred in the application at location 0x7c812afb. 


--------------------------- 
OK   Cancel    
---------------------------  

What can be causing this error. Is there a upper limit on the number of .Net service you can run on a windows server ?

I have asked this on ServerFault already. The link is here

The code:

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;

namespace TestService
{
    public class MyService : ServiceBase
    {
        private Timer stateTimer;
        private readonly TimerCallback timerDelegate = WriteToLog;
        static readonly string fileName = @"C:\temp\Testservice\Log\" + new Random().Next() + ".txt";
        public MyService()
        {
            ServiceName = "Test Service";
            CanStop = true;
            CanPauseAndContinue = false;
            AutoLog = true;
        }

        protected override void OnStart(string[] args)
        {
            using (StreamWriter writer = new StreamWriter(fileName, true))
            {
                writer.WriteLine("Starting ...");
                writer.WriteLine(Assembly.GetExecutingAssembly().Location);
            }
            stateTimer = new Timer(timerDelegate, null, 1000, 1000);
        }

        protected override void OnStop()
        {
            stateTimer.Dispose();
        }

        static void WriteToLog(object stateObject)
        {
            using (StreamWriter writer = new StreamWriter(fileName, true))
            {
                writer.WriteLine(DateTime.Now.Ticks);
            }
        }

        public static void Main()
        {
            Run(new MyService());
        }
    }
}

Best Answer

You're probably running into the well-known desktop heap limitation, which by default limits the amount of memory related to user-interface and desktop-related that things can consume to 48 MB. A typical service probably consumes a few hundred kilobytes of desktop heap, so it's not surprising that you can only start 20 or so given the other apps that are running on your system.

For more info, see this excellent ntdebugging article.