简体   繁体   中英

Can't start topshelf service locally: The service did not respond in a timely fashion

I am having difficulties to start my service using the following setup.

static void Main(string[] args)
{
    var builder = new ContainerBuilder();
    builder.RegisterModule<FixAcceptorModule>();
    var container = builder.Build();

    var rc = HostFactory.Run(c =>
    {
        c.UseAutofacContainer(container);
        c.Service<IServiceManager>(svc =>
        {
            svc.ConstructUsingAutofacContainer();
            svc.WhenStarted(sm => sm.Start()).BeforeStartingService(a => a.RequestAdditionalTime(TimeSpan.FromSeconds(30)));
            svc.WhenStopped(sm => sm.Stop());
        });

        c.SetDescription("");
        c.SetDisplayName("FIX Acceptor Service");
        c.SetServiceName("FixAcceptorSvc");

        c.RunAsLocalService();
        c.StartManually();
    });
}

This is the output of the start action myservice.exe start :

Topshelf.Hosts.StartHost Error: 0 : The service failed to start., System.InvalidOperationException: Cannot start service FixAcceptorSvc on computer '.'. ---> System.ComponentModel.Win32Exception: The service did not respond to the start or control request in a timely fashion
   --- End of inner exception stack trace ---
   at System.ServiceProcess.ServiceController.Start(String[] args)
   at System.ServiceProcess.ServiceController.Start()
   at Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String serviceName, TimeSpan startTimeOut)
   at Topshelf.Hosts.StartHost.Run()

Although the exception points to a timeout I don't think this is related to a timing issue, as this exception get's thrown immediately. I guess it's more like a permission or configuration issue.

Things to note:

  • My developer machine is a domain joined and runs Windows 10 Pro - 1909
  • When starting the service from Visual Studio 2019 in Debug configuration everything works perfectly
  • myservice.exe install --localservice (and c.RunAsLocalSystem() in HostFactory ) => same result
  • myservice.exe install --localsystem (and c.RunAsLocalSystem() in HostFactory ) => same result
  • Also using myservice.exe install -username myaduser -password myadpassword did not help (double checked group policies and that my user is allowed to logon as a service)

So what am I doing wrong here?

The mentioned exception is totally misleading. I found the culprit. To get more details I've added the following action:

c.OnException(ex =>
{
    File.AppendAllText(@"C:\Temp\Service.txt", ex.ToString());
});

Now this finally got me on the track. As the exception shows there is something wrong with my service, which is looking in the wrong folder ( C:\Windows\System32 ) for the FIX dictionary... Problem solved!

Topshelf.ServiceBuilderException: An exception occurred creating the service: IServiceManager ---> Autofac.Core.DependencyResolutionException: An exception was thrown while activating StrukiApp.FixAcceptor.Infrastructure.ServiceManager -> StrukiApp.FixAcceptor.Acceptor. ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(QuickFix.IApplication, QuickFix.IMessageStoreFactory, QuickFix.SessionSettings, QuickFix.ILogFactory)' on type 'Acceptor'. ---> QuickFix.ConfigError: Configuration failed: Could not find file 'C:\WINDOWS\system32\FIX44.xml'. ---> System.IO.FileNotFoundException: Could not find file 'C:\WINDOWS\system32\FIX44.xml'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at QuickFix.SessionFactory.createDataDictionary(SessionID sessionID, Dictionary settings, String settingsKey, String beginString)
   at QuickFix.SessionFactory.ProcessFixDataDictionary(SessionID sessionID, Dictionary settings, DataDictionaryProvider provider)
   at QuickFix.SessionFactory.Create(SessionID sessionID, Dictionary settings)
   at QuickFix.ThreadedSocketAcceptor.CreateSession(SessionID sessionID, Dictionary dict)
   at QuickFix.ThreadedSocketAcceptor.CreateSessions(SessionSettings settings, SessionFactory sessionFactory)
   at QuickFix.ThreadedSocketAcceptor..ctor(SessionFactory sessionFactory, SessionSettings settings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM