akrisanov
akrisanov

Reputation: 3214

Installing the console application as a Windows service

I'm writing a simple Windows Service based on TopShelf. How to install my application as a service? I tried to execute SpyService.exe install, but it doesn't work.

What is the difference between next two ways of configuring the service?

var cfg = RunnerConfigurator.New(
    x =>
    {
        x.ConfigureService<SpyService>(s =>
        {
            s.Named("SpyService");
            s.HowToBuildService(name => new SpyService());
            s.WhenStarted(tc => { 
                XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config")); 
                tc.Start(); });
            s.WhenStopped(tc => tc.Stop());
        });
        x.RunAsFromInteractive();

        x.SetDescription("Сервис логирования действий пользователя.");
        x.SetDisplayName("SpyService");
        x.SetServiceName("SpyService");
    });

Runner.Host(cfg, args);

and

var host = HostFactory.New(x =>
{                
    x.Service<SpyService>(s =>
    {
        s.SetServiceName("SpyService");
        s.ConstructUsing(name => new SpyService());
        s.WhenStarted(service =>
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
            service.Start();
        });
        s.WhenStopped(service => service.Stop());
    });

    x.RunAsLocalSystem();
    x.SetDescription("Сервис логирования действий пользователя.");
    x.SetDisplayName("SpyService");
    x.SetServiceName("SpyService");
});

host.Run();

I noticed that if I use the second method the service is successfully installed, but there is not possible to start the service with x.RunAsFromInteractive() as in first way.

Upvotes: 2

Views: 2250

Answers (3)

Liang Wu
Liang Wu

Reputation: 1822

You can run the Console Command as Administrator first, then run the install command

Upvotes: 0

Hoang Tran
Hoang Tran

Reputation: 481

The first approach is obsoleted in the latest version (2.2), afaik.

Regarding RunAsFromInteractive(), looking at the topshelf source code, I see that it called RunAs() with empty username/password:

public void RunAsFromInteractive()
{
    this.RunAs("", "");
}

Upvotes: 0

Travis
Travis

Reputation: 10547

What version of Topshelf are you using? The old syntax was SpyService.exe service install but has been simplified.

Upvotes: 1

Related Questions