Topshelf timeout issue
Asked Answered
A

2

8

We are using Topshelf to host a service. Before starting the service, we are making database call to load lot of data. Because of this, while starting the service, we are getting following error:

Start Service failed with return code '[7] ServiceRequestTimeout

We are using following code to start the service:

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

If I try to launch the service using Visual Studio, service runs fine. But when the service is hosted through Topshelf, I am getting time out error.

I have also tried using hostControl.RequestAdditionalTime(TimeSpan.FromSeconds(300)) but even after adding additional timeout period, I am not able to resolve the issue. Please provide your suggestions.

Abram answered 25/10, 2013 at 12:17 Comment(1)
topshelf-4 gives you x.SetStartTimeOut(Timespan) and x.SetStopTimeout(TimeSpan)Comfortable
E
14

What the documentation for HostControl.RequestAdditionalTime fails to state is that you can only ask for a max of 60 or 120 seconds. Otherwise it ignores your request.

It's brilliantly documented absolutely no where that I'm aware of :( If you find it documented some where, please let me know.

Extraordinary answered 25/10, 2013 at 17:24 Comment(5)
Thank you for the reply Travis. I tried setting 120 as AdditionalTime but even that is not working.Abram
nope. I will check and let you know :)But I tried 120 for sure and that did not workedAbram
Could you use a Task to fetch the database and then continue with the actual start behavior? Task.Run takes almost zero time...Bakerman
@Travis: I don't know much about Windows services... I am just curious to know, why do you say that we can only pass 60 or 120 seconds? I had a quick look at the Source code, to verify this, but cannot find anything...Kreiner
@Hooman It was a limitation of the underlying Windows Service Manager (Windows, not .NET API), I am unclear if it is still a limitation on newer server/desktop versions since this was from 2013 and I haven't been keeping up on that.Extraordinary
C
1

Here be Dragons

from TopShelf

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

        RequestAdditionalTime((int)timeRemaining.TotalMilliseconds);
    }

And here is JustDecompile from ServiceBase

    /// <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);
            }
        }
    }
Comfortable answered 8/11, 2016 at 23:18 Comment(1)
I can't upvote as I know nothing of the API, but LOL @ "Here be Dragons".Corettacorette

© 2022 - 2024 — McMap. All rights reserved.