0

所以我正在尝试运行我编写的服务,由于某种原因,当我尝试启动它时它给了我这个错误:

Error 1053: the service did not respond to the start or control request in a timely fashion

我的代码非常基本。

static void Main(string[] args)
    {
        try
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Program() 
            };
            ServiceBase.Run(ServicesToRun);
        }
        catch (Exception ex)
        {
            string SourceName = "WindowsService.ExceptionLog";
            if (!EventLog.SourceExists(SourceName))
            {
                EventLog.CreateEventSource(SourceName, "Application");
            }

            EventLog eventLog = new EventLog();
            eventLog.Source = SourceName;
            string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace);
            eventLog.WriteEntry(message, EventLogEntryType.Error);
        }
    }

    public Program()
    {
        this.ServiceName = "FetchFeed";
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

        //TODO: place your start code here
    repeat: FetchFeed();
        Thread.Sleep(3600000);
        goto repeat;
    }

    protected override void OnStop()
    {
        base.OnStop();

        //TODO: clean up any variables and stop any threads
    }

private static void FetchFeed()
{
    //Some HTTP requests and retrieval.
}

这是安装程序类:

[RunInstaller(true)]
public class Service_Installer : Installer
{
    public Service_Installer()
    {
        ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
        ServiceInstaller serviceInstaller = new ServiceInstaller();

        //set the privileges
        processInstaller.Account = ServiceAccount.LocalSystem;

        serviceInstaller.DisplayName = "FetchFeed";
        serviceInstaller.StartType = ServiceStartMode.Manual;

        //must be the same as what was set in Program's constructor
        serviceInstaller.ServiceName = "FetchFeed";

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);
    }
}

错误背后的原因可能是什么?我已经检查了 FetchFeed() 是否可以作为一个没有异常的独立应用程序工作。

4

2 回答 2

1
repeat: FetchFeed();
Thread.Sleep(3600000);
goto repeat;

这是你的问题。Windows 服务应在 30 秒内响应,因此 Windows 知道它已成功启动。

使用此代码,您将阻塞唯一的线程,并且服务不再响应任何内容。

尝试在不同的线程或其他方式上执行此操作。

我会建议另一种方式,因为使用gotoa 真的不行:)

还可以查看有关 Windows 服务的一些教程和基本文档以更好地理解。

于 2012-08-06T13:44:38.283 回答
1

使用合适的计时器

public class YourService
{
       var tim = new System.Timers.Timer(60 * 60 * 1000); // 1 hour

    protected override void OnStart(string[] args)  
    {  
        base.OnStart(args);  

        tim.AutoReset = true;
        tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed);
        tim.Enabled = true;
        // first time run
        ThreadPool.QueueUserWorkItem(WaitCallback(FetchFeed));
    }

    static void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
          FetchFeed();
    }
}
于 2012-08-06T14:12:45.990 回答