【问题标题】:C# Windows Service does not appear as startedC# Windows 服务未显示为已启动
【发布时间】:2014-11-05 08:59:15
【问题描述】:

我用 C# 编写了一个小 Windows 服务,它应该侦听特定端口并对请求执行某些操作。

我也这样写了 Main:

using System;
using System.ServiceProcess;

namespace AutoDeployService
{
    public static class WindowsServiceController
    {
        private static void Main(string[] args)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new AutoDeployService() 
            };

            if (Environment.UserInteractive)
            {
                new AutoDeployService().ConsoleRun();
            }
            else
            {
                System.Diagnostics.Debugger.Break();
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
}

所以我可以使用控制台启动服务,也可以不使用。当我将程序作为控制台应用程序启动时,它工作正常!

如果我使用“InstallUtil.exe”安装服务,它会完美安装并出现在服务列表中。但是当我尝试启动服务时,它会在大约半分钟或更长时间后给我这个消息:

服务没有及时响应启动或控制请求。

我在 www 中研究了这个错误后发现,OnStart-Method 可能需要很长时间,现在我的 OnStart-Method 看起来像:

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            var initializeThread = new Thread(new ThreadStart(Initialize));
            initializeThread.Start();
        }

所以我希望任何人都可以帮助我。

问候

编辑: 我用servicemanager启动他后服务似乎启动了,但是在servicemanager中出现上述错误并且定义为未启动。

事件日志:

名称 der fehlerhaften Anwendung:AutoDeployService.exe,版本:1.0.0.0,Zeitstempel:0x5459e290 名称 des fehlerhaften 模块:未知,版本:0.0.0.0,Zeitstempel:0x00000000 Ausnahmecode: 0x00000000 Fehleroffset: 0x0039010b ID des fehlerhaften Prozeses: 0xa0c Startzeit der fehlerhaften Anwendung: 0x01cff8d816b830d8 Pfad der fehlerhaften Anwendung: C:\Users*用户名*\Desktop\AutoDeploy\AutoDeployService\bin\Debug\AutoDeployService.exe Pfad des fehlerhaften 模块:未知 Berichtskennung: 5d636ccf-64cb-11e4-b5d1-0050568bc9b7

编辑:

如果我启动服务,服务似乎没有进入 OnStart-Method,因为他没有创建文件夹。

【问题讨论】:

  • 检查应用程序的 Windows 事件日志。另请注意,最好检查是否附加了调试器If (Debugger.IsAttached) System.Diagnostics.Debugger.Break();
  • @Silvermind 感谢您的快速重播。事件日志中的消息没有多大帮助。请在我的帖子中查看编辑。

标签: c# windows service onstart


【解决方案1】:

似乎有些误会;虽然可执行文件可以从命令行启动,但服务本身不能从命令行启动。此外,关于服务创建的教程可以在here找到。

【讨论】:

  • 是的,对不起,这就是我要说的,如果我从控制台启动可执行文件,它工作正常。所以错误必须与服务有关。谢谢您的答复。我查看了本教程,但对我帮助不大。我的服务似乎是对的。
【解决方案2】:

感谢您的帮助。

我发现我的错误是使用以下命令附加调试器:

Debugger.Launch().

错误出现在我的应用程序的线程中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    相关资源
    最近更新 更多