开发者

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.Ru开发者_JAVA技巧nAsLocalSystem();
    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("", "");
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜