Tra*_*vis 7

TopShelf的新位置http://github.com/Topshelf/Topshelf已更新为允许此行为的补丁.

RunConfiguration cfg = RunnerConfigurator.New(x =>
{
    x.AfterStoppingTheHost(h => { Console.WriteLine("AfterStop called invoked, services are stopping"); });

    x.ConfigureService<TownCrier>(s =>
    {
        s.Named("tc");
        s.HowToBuildService(name=> new TownCrier());
        s.WhenStarted(tc => tc.Start());
        s.WhenStopped(tc => tc.Stop());
    });
    // Running as the network service account
    x.RunAsNetworkService();

    x.SetDescription("Sample Topshelf Host");
    x.SetDisplayName("Stuff");
    x.SetServiceName("stuff");
});

Runner.Host(cfg, args);
Run Code Online (Sandbox Code Playgroud)

  • 您还可以使用命令行选项覆盖配置,例如--networkservice, - localservice或--interactive,以在安装服务期间提示输入用户名/密码. (3认同)