简体   繁体   English

C# Topshelf 超时异常

[英]C# Topshelf TimeoutException

As a First step I created Windows Service project configured it properly and作为第一步,我创建了 Windows 服务项目并对其进行了正确配置

On second Step I have added TopShelf Version 3.1.135.0 in my project If I run my service through (F5 Run) then it is loading Top-shelf Console and service is completed successfully.在第二步中,我在我的项目中添加了TopShelf版本 3.1.135.0如果我通过 (F5 Run) 运行我的服务,那么它正在加载 Top-shelf 控制台并且服务已成功完成。

However When I am running it to install and Start it from command prompt I am having below TimeOut Error.但是,当我运行它以安装并从命令提示符启动它时,我遇到了超时错误。

Topshelf.Hosts.StartHost Error: 0 : The service failed to start., System.Service
Process.TimeoutException: Time out has expired and the operation has not been co
mpleted.



 public class AppService
    {
        LoggingService loggingService = new LoggingService(typeof(AppService).Name);


        public void Start()
        {
            loggingService.Info("SampleService is Started");
            ExtractProcess.Start();
            TransformProcess.Start();

        }

        public void Stop()
        {
            loggingService.Info("SampleService is Stopped");

        }
    }

-- Updated Code to fix this issue -- 更新代码以解决此问题

 public void Start()
    {
        loggingService.Info("MPS.GOA.ETLService  is Started");
        ThreadStart myThreadDelegate = new ThreadStart(StartService);
        Thread myThread = new Thread(myThreadDelegate);
        myThread.Start();

    }

private void StartService()
{
    timer.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsedTime);
    timer.Interval = 60000 * ServiceIntervalInMinutes;     //1 minute 60000 milliseconds
    timer.Enabled = true;
    Process();
}

private void Process()
{
    ExtractProcess.Start();
    TransformProcess.Start();
}

Any Suggestions?有什么建议?超时错误

This error is happening because you are running the extract and process methods in the Start method of the service. 发生此错误的原因是您正在服务的Start方法中运行提取和处理方法。 This is OK in Visual Studio, but when you install the service and start it, the Service Control Manager waits for the Start method to return, and if it does not do so within a certain time (30 seconds by default) then it will return this error. 在Visual Studio中可以,但是当您安装服务并启动它时,服务控制管理器会等待Start方法返回,如果它在一定时间内没有这样做(默认为30秒),那么它将返回这个错误。

You have several options, all of which will allow the Start method to return immediately: 您有几个选项,所有这些选项都允许Start方法立即返回:

  1. Invoke the extract and transform methods on a separate thread 在单独的线程上调用提取和转换方法
  2. Invoke the extract and transform methods asynchronously 异步调用提取和转换方法
  3. Use a timer to start the extract and transform process 使用计时器启动提取和转换过程

In case you (like me) is struggling to get the service to start - and all you've found so far is references to starting work in a separate thread (and you already did) this might be the solution right here .. 如果你(像我一样)正在努力让服务开始 - 你到目前为止所发现的只是引用在一个单独的线程中开始工作(你已经做过), 这可能就是这里的解决方案 ..

My problem was that I had an external JSON config file being read from the project's directory path. 我的问题是我从项目的目录路径中读取了一个外部JSON配置文件。 What I needed was to get the assembly path, so that when the .NET application is published and installed with Topshelf - it looks for the config file at the right place. 我需要的是获取程序集路径,以便在.NET应用程序发布并与Topshelf一起安装时 - 它会在正确的位置查找配置文件。

string assemblyPath = Path.GetDirectoryName(typeof(MyConfigManagerClass).Assembly.Location);

var builder = new ConfigurationBuilder()
    .SetBasePath(assemblyPath)
    .AddJsonFile("config.json", optional: false);

myConfigurationObject = builder.Build();

Topshelf gave an error saying the service couldn't be started, but now I finally know why. Topshelf发出错误说该服务无法启动,但现在我终于明白了原因。

In my case it was neither of the above solutions that solved it, but actual permissions within the topshelf service, that required access to a file that resided in an external server.在我的情况下,上述解决方案都没有解决它,而是topshelf服务中的实际权限,需要访问驻留在外部服务器中的文件。

TopShelf program running on test server在测试服务器上运行的 TopShelf 程序

Log file located on Production server位于生产服务器上的日志文件

Test server does not have access to external servers, for security reasons.出于安全原因,测试服务器无权访问外部服务器。

So I changed the program to refer everything internally inside it's own server, and it worked fine.因此,我更改了程序以在其自己的服务器内部引用所有内容,并且运行良好。

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

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