将控制台应用程序安装为 Windows 服务

发布于 2024-10-27 06:07:49 字数 1604 浏览 1 评论 0原文

我正在编写一个基于 TopShelf 的简单 Windows 服务。如何将我的应用程序安装为服务?我尝试执行SpyService.exe install,但它不起作用。

接下来两种配置服务的方式有什么区别?

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);

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();

注意到,如果我使用第二种方法,服务已成功安装,但无法像第一种方法那样使用 x.RunAsFromInteractive() 启动服务。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北风几吹夏 2024-11-03 06:07:49

您使用的是哪个版本的 Topshelf?旧语法是SpyService.exe service install,但已被简化。

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

梦太阳 2024-11-03 06:07:49

afaik,第一种方法在最新版本(2.2)中已过时。

关于 RunAsFromInteractive(),查看 topshelf 源代码,我看到它使用空用户名/密码调用 RunAs():

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

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("", "");
}
左岸枫 2024-11-03 06:07:49

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文