简体   繁体   English

Topshelf 不调用服务启动

[英]Topshelf does not call service start

I am seeing different behaviour in Topshelf depending on whether I start the application in standalone mode or installed as a service.我在 Topshelf 中看到不同的行为,具体取决于我是以独立模式启动应用程序还是作为服务安装。 I have tried Topshelf 3.3.1 and 4.1.我试过 Topshelf 3.3.1 和 4.1。

I am using a service that implements ServiceControl, which works fine in standalone mode.我正在使用实现 ServiceControl 的服务,该服务在独立模式下运行良好。 When trying to start the installed service, I receive the message that the service took too long to respond to control requests.尝试启动已安装的服务时,我收到该服务响应控制请求的时间过长的消息。

var resultCode = HostFactory.Run(x =>
{
    x.Service<ServiceControl>(sc =>
    {
        sc.ConstructUsing(() =>
        {
            Console.WriteLine("GET INSTANCE!");
            return new WorkerService();
        });
        sc.WhenStarted((s, h) =>
        {
            Console.WriteLine("START!");
            return s.Start(h);
        });
        sc.WhenStopped((s, h) =>
        {
            Console.WriteLine("STOP!");
            return s.Stop(h);
        });
        sc.BeforeStartingService(() => Console.WriteLine("BEFORE START!"));
    });

    x.SetDescription("WorkerService");
    x.SetDisplayName("WorkerService");
    x.SetServiceName("WorkerService");
});

This is a very verbose version of calling a service (with lots of console output), but even the calls to ConstructUsing or BeforeStartingService do not produce any console output, while log calls inside the Service<> lambda produce output.这是调用服务的一个非常冗长的版本(有很多控制台输出),但即使是对ConstructUsingBeforeStartingService的调用也不会产生任何控制台输出,而Service<> lambda 中的日志调用会产生输出。

I am left quite clueless here, but also have not been able to reproduce this with a minimal sample.我在这里一无所知,但也无法用最少的样本重现这一点。 The timeout comes after about 3-4 seconds, without any visible attempt to start the service.大约 3-4 秒后超时,没有任何可见的启动服务尝试。 Starting the service thread from the program main does work without exception.从程序 main 启动服务线程确实可以正常工作。

Starting the service with WorkerService.exe works as expected, starting it with WorkerService.exe start (which starts the service) does not work.使用WorkerService.exe启动服务按预期工作,使用WorkerService.exe start (启动服务)启动它不起作用。

As this happens with Topshelf 3 and 4, it most likely is something I am doing wrong inside the application.由于 Topshelf 3 和 4 发生这种情况,很可能是我在应用程序中做错了。 Any pointer into the right direction is very much appreciated.任何指向正确方向的指针都非常感谢。

Have you registered WorkerService as ServiceControl? 您是否已将WorkerService注册为ServiceControl? If not, register the service. 如果没有,请注册服务。 (Do you use Autofac?). (您是否使用Autofac?)。 If not, try providing 如果没有,请尝试提供

x.Service<WorkerService>(sc => // Your code

instead of ServiceControl 代替ServiceControl

I have a lead now: as part of loading the config we are loading a file from the roaming AppData and it looks like if the file is not accessible, an exception is thrown and silently discarded. 我现在处于领先地位:作为加载配置的一部分,我们从漫游AppData加载文件,并且看起来如果该文件不可访问,则会引发异常并以静默方式将其丢弃。 If the file does not exist, the service starts as expected and everything works. 如果该文件不存在,该服务将按预期启动,并且一切正常。

So the basic problem is that an uncaught exception was thrown before the Topshelf service config part was started, which seems to be used to forward some output to the command line application when calling the exe with the start parameter. 因此,基本问题是在启动Topshelf服务配置部分之前引发了未捕获的异常,该异常似乎用于在使用start参数调用exe时将某些输出转发到命令行应用程序。

I will try to investigate on that as time allows and to contribute to Topshelf with the learnings. 我将尝试在时间允许的情况下对此进行调查,并通过学习为Topshelf做出贡献。

Try to register the service in cmd (with admin privileges):尝试在 cmd 中注册服务(具有管理员权限):

to install: yourApplication.exe install安装: yourApplication.exe install
to unistall: yourApplication.exe uninstall卸载: yourApplication.exe uninstall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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