8

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)) ましたが、タイムアウト期間を追加した後でも、問題を解決できません。あなたの提案を提供してください。

4

2 に答える 2

0

こちらがドラゴンズ

トップシェルフから

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);
            }
        }
    }
于 2016-11-08T23:18:14.193 に答える