问题
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.
回答1:
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.
回答2:
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);
}
}
}
来源:https://stackoverflow.com/questions/19589686/topshelf-timeout-issue