【问题标题】:Why can't I start more than X number of windows service developed in .Net on one machine为什么我不能在一台机器上启动超过 X 个在 .Net 中开发的 Windows 服务
【发布时间】:2010-01-12 03:03:30
【问题描述】:

我偶然发现了无法启动超过一定数量的 .Net Windows 服务的情况。我们的软件系统是一组 8-9 个 Windows 服务,我想在一台机器上创建 5 个逻辑环境(即大约 40 个服务)。为了重现问题并证明它不是应用程序问题,我创建了一个简单的 Windows 服务。

我将其编译为 TestService.exe,然后创建 30 个副本。从 TestService01.exe 到 TestService30.exe。 我正在使用 SC 安装服务,如下所示

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

现在我可以在 Windows Server 2003 机器上启动 15 到 25 个这些服务。之后我无法启动其他服务。它失败并显示错误消息框

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


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

可能导致此错误的原因。您可以在 Windows 服务器上运行的 .Net 服务的数量是否有上限?

我已经在 ServerFault 上问过这个问题。链接是here

代码:

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());
        }
    }
}

【问题讨论】:

    标签: .net windows-services


    【解决方案1】:

    您可能遇到了众所周知的桌面堆限制,它默认将与用户界面和桌面相关的内存量限制为 48 MB。一个典型的服务可能会消耗几百 KB 的桌面堆,因此考虑到系统上运行的其他应用程序,您只能启动 20 个左右也就不足为奇了。

    有关详细信息,请参阅 this excellent ntdebugging article

    【讨论】:

    • 是的,我读过那篇文章。但我仍然不明白这是否与.Net 和/或域帐户有关。例如如果我停止“Windows 时间服务”然后启动 15 的 myservice,然后第 16 次没有启动,我仍然可以启动我之前停止的“Windows 时间服务”。这似乎不会影响内置的 Windows 服务。
    • 所以尝试使用不同的用户来运行每个服务。或本地/服务帐户。
    • 使用不同的服务帐户是行不通的(并且经过验证)。如果您阅读这篇文章,服务的每个实例都会创建一个会话并占用 512KB 的桌面堆空间。使用 dheapmon 来验证这一点。我将尝试一些注册表设置更改。
    • 我接受了显示正确解释的答案,尽管这仍然不能完全帮助我。我已尝试更改注册表设置以将桌面堆大小设置为 128Mb,它似乎可以正常工作。但这不是一个好的解决方案,因为它会通过限制堆大小来影响所有运行的服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2019-12-19
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    相关资源
    最近更新 更多