Topshelf: install command does not return after successfully installing the service
Asked Answered
E

2

6

NOTE: I'm not doing anything similar to Topshelf installer requires me to press enter twice - why?

Service class (interesting parts):

public class ServiceCore
{
    public ServiceCore(ServiceRuntimeConfiguration serviceRuntimeConfiguration)
    {
        _runningTasks = new List<Task>();
    }

        public bool Start(HostControl hostControl)
        {
            _hostControl = hostControl;
            _messageProcessor.Start(); // Starts a System.Threading.Tasks.Task
            StartListener(); // starts a System.Threading.Tasks.Task
            return true;
        }
}

Program.cs:

Host host = HostFactory.New(configurator =>
{

configurator.UseNLog();

// Configure core service
configurator.Service<ServiceCore>(svc =>
{
    svc.ConstructUsing(theService => new ServiceCore(_serviceRuntimeConfiguration));
    svc.WhenStarted((svc, hostControl) => svc.Start(hostControl));
    svc.WhenStopped((svc, hostControl) => svc.Stop(hostControl));
});

// Configure recovery params
configurator.EnableServiceRecovery(recoveryConfigurator =>
{
    recoveryConfigurator.RestartService(0);
    recoveryConfigurator.OnCrashOnly();
    recoveryConfigurator.SetResetPeriod(1);
});

// Execute HostConfigurator
host.Run();
}

The Problem

When I do this:

MyService.exe install --manual --localsystem

The service installs fine, but the command never returns:

Running a transacted installation.

Beginning the Install phase of the installation. Installing service NotificationEngine.Main... Service NotificationEngine.Main has been successfully installed.

The Install phase completed successfully, and the Commit phase is beginning.

The Commit phase completed successfully.

The transacted install has completed.

^C (I have to press CTRL+C)

What should I do for the install command to complete and then return?

NOTE The same behaviour is observable if I run help (i.e. help displays but the command does not return):

MyService.exe help
Escarpment answered 22/1, 2016 at 4:31 Comment(0)
M
2

Generally this means you don't release control of some resource and the process can't cleanly exit. However, this stuff is complicated, so it's hard to say for sure.

A few things I would try

  • What happens when you execute MyService start after an install/CTRL+C? I'm assuming it also blocks since help does.
  • Check logging, do you have any enabled? Is there file contention or permissions issues?
  • What else does your Main() entry point do? Is it doing something that after host.Run()? Your code above makes it looks like you're calling it from within the construction of that object, but I assume it's bad cut-n-pasting.
  • Make sure you aren't initializing resources before the ConstructUsing and When* callbacks are fired.

After this, I would take this to our mailing list at https://groups.google.com/forum/#!forum/topshelf-discuss.

Marsala answered 23/1, 2016 at 13:59 Comment(1)
Thanks for your elaborate answer. I'll check and post back.Escarpment
D
0

ServiceCore : ServiceBase

The type T specified in the configurator.Service should subclass ServiceBase.

This fixed the problem for a service that would install fine but hang on the last step of install/uninstall.

Diffusion answered 6/11, 2018 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.