简体   繁体   中英

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.

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

您可以先以管理员身份运行控制台命令,然后再运行安装命令

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("", "");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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