views:

51

answers:

1

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());
        }
    }
}
+1  A: 

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.

John Feminella
Yes I have read that article. But I still don't understand if it is something to do with .Net and/or domain accounts. e.g. If I stop "Windows time service" then start 15 of myservice and then 16th doesn't start, I can still start the "Windows Time Service" I have stopped earlier. This doesn't seem to affect the builtin windows services.
Pratik
So try using a different user to run each service. Or local/service account.
No Refunds No Returns
Using different service accounts wouldn't work (and verified). If you read the article every instance of a service creates a session and chews up 512KB of desktop heap space. Used dheapmon to verify that. I am going to try some of the registry setting changes.
Pratik
I am accepting the answer for showing the correct explanation although that still doesn't help me fully. I have tried the registry setting changes to set the desktop heap size to 128Mb an it seems to be working. But this is not a good solution as it affects all services running by limiting the heap size.
Pratik