Topshelf超时问题

时间:2013-10-25 12:17:45

标签: .net windows-services topshelf

我们正在使用Topshelf来托管服务。在启动服务之前,我们正在进行数据库调用以加载大量数据。因此,在启动服务时,我们遇到以下错误:

Start Service failed with return code '[7] ServiceRequestTimeout

我们正在使用以下代码来启动服务:

HostFactory.Run(x =>
            {
                x.Service<AppService>(s =>
                {
                    s.ConstructUsing(name => new AppService(s_resolver, baseAddress, resolver));
                    s.WhenStarted(svc => svc.Start());
                    s.WhenStopped(svc => svc.Stop());
                    s.WhenShutdown(svc => svc.Shutdown());
                });

                x.EnableShutdown();
                x.RunAsLocalService();
                x.StartAutomatically();
                x.SetDisplayName("Application Host");
                x.SetDescription("Application Host");
            });

如果我尝试使用Visual Studio启动服务,则服务运行正常。但是当服务通过Topshelf托管时,我就会出错。

我也尝试过使用hostControl.RequestAdditionalTime(TimeSpan.FromSeconds(300))       但即使在添加额外的超时期限后,我也无法解决问题。请提供您的建议。

2 个答案:

答案 0 :(得分:9)

HostControl.RequestAdditionalTime的文档未能说明的是,您最多只能要求60或120秒。否则它会忽略您的请求。

它的出色记录绝对没有我所知道的地方:(如果你发现它记录在哪里,请告诉我。

答案 1 :(得分:0)

这里是龙族

来自TopShelf的

void HostControl.RequestAdditionalTime(TimeSpan timeRemaining)
    {
        _log.DebugFormat("Requesting additional time: {0}", timeRemaining);

        RequestAdditionalTime((int)timeRemaining.TotalMilliseconds);
    }

这是来自ServiceBase的JustDecompile

    /// <summary>Requests additional time for a pending operation.</summary>
    /// <param name="milliseconds">The requested time in milliseconds.</param>
    /// <exception cref="T:System.InvalidOperationException">The service is not in a pending state.</exception>
    [ComVisible(false)]
    public void RequestAdditionalTime(int milliseconds)
    {
        unsafe
        {
            fixed (NativeMethods.SERVICE_STATUS* sERVICESTATUSPointer = &this.status)
            {
                if (this.status.currentState != 5 && this.status.currentState != 2 && this.status.currentState != 3 && this.status.currentState != 6)
                {
                    throw new InvalidOperationException(Res.GetString("NotInPendingState"));
                }
                this.status.waitHint = milliseconds;
                this.status.checkPoint = this.status.checkPoint + 1;
                NativeMethods.SetServiceStatus(this.statusHandle, sERVICESTATUSPointer);
            }
        }
    }