C # Topshelf TimeoutException

As a first step, I created a Windows service project, set it up correctly, and

In the second step, I added TopShelf Version 3.1.135.0 in my project. If I started my service through (F5 Run), then the top-level console loads and the service is successfully completed.

However, when I run it for installation and run it from the command line, I have a lower 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 problem.

  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? Time Out Error

+7
c # windows-services topshelf
source share
2 answers

This error is due to the fact that you run the extraction and processing methods in the Start method of the service. This is normal in Visual Studio, but when you install the service and start it, the service control manager expects the Start method to return, and if it does not do it within a certain time (30 seconds by default), it will return this error.

You have several options, all of which will allow the Start method to return immediately:

  • Call extraction and conversion methods in a separate thread
  • Activate extraction and conversion methods asynchronously
  • Use the timer to start the extraction and conversion process.
+12
source share

If you (like me) struggle to start the service, and all you have found so far are links to work in a separate thread (and you already did), this may be the solution right here .

My problem was that I had an external JSON configuration file that is being read from the project directory path. I needed to get the build path, so when the .NET application is published and installed using Topshelf, it looks for the configuration file in the right place.

 string assemblyPath = Path.GetDirectoryName(typeof(MyConfigManagerClass).Assembly.Location); var builder = new ConfigurationBuilder() .SetBasePath(assemblyPath) .AddJsonFile("config.json", optional: false); _config = builder.Build(); 

Topshelf gave an error saying that the service could not be started, but now I finally know why.

0
source share

All Articles