C# Topshelf TimeoutException
Asked Answered
P

3

12

As a First step I created Windows Service project configured it properly and

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.

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

Posterity answered 14/1, 2015 at 10:36 Comment(4)
What does AppService.Start() do?Psychosurgery
I have added code for it. But issue is if I run from VS F5 service does job properly it is only when I install and run it gives me TimeOut ErrorPosterity
Extract process reads a file from network location and loads into databasePosterity
Why do you need a timer in the updated/fixed code?Meekins
H
17

This error is happening because you are running the extract and process methods in the Start method of the service. 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.

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

  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
Harakiri answered 15/1, 2015 at 15:43 Comment(2)
Thanks, I fixed it as you suggested Please see my update. am I following best practise?Posterity
Looks OK to me. You could do it without the ThreadStart, but it isn't doing any harm.Harakiri
S
6

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. 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.

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.

Spirochete answered 5/12, 2018 at 13:24 Comment(1)
I faced this exactly problem with .net core. This solution has solved my problem. Great.Burrstone
M
0

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 program running on test server

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.

Misspend answered 23/12, 2021 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.