让Topshelf服务在启动时运行一次,然后停止

时间:2014-04-30 14:39:07

标签: windows-services topshelf

我已经编写了一个TopShelf服务/控制台应用程序,它似乎按预期运行,除了我希望它在启动时运行一次,然后在下次启动/重启之前禁用它。

我希望这会奏效:

class MyServiceClass
{
    public void Start()
    {
        // do the things that need doing

        this.Stop();
    }

    public void Stop()
    {
    }

但这并不起作用,大概是因为this.Stop()命令用于清理,而不是导致服务停止。

我的Program.cs看起来像这样:

// Uses Topshelf: http://topshelf-project.com/
// Guided by: http://www.ordina.nl/nl-nl/blogs/2013/maart/building-windows-services-with-c-and-topshelf/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Topshelf;

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(hostConfigurator =>
            {
                hostConfigurator.Service<MyServiceClass>(serviceConfigurator =>
                {
                    serviceConfigurator.ConstructUsing(() => new MyServiceClass());
                    serviceConfigurator.WhenStarted(myServiceClass => myServiceClass.Start());
                    serviceConfigurator.WhenStopped(myServiceClass => myServiceClass.Stop());
                });

                hostConfigurator.RunAsLocalSystem();

                hostConfigurator.SetDisplayName("MyService");
                hostConfigurator.SetDescription("Does stuff.");
                hostConfigurator.SetServiceName("MyService");
                hostConfigurator.StartAutomatically();
                hostConfigurator.EnableShutdown();
            });
        }
    };
}

如何在执行结束时停止服务?

更新:根据Damien的意见,我现在有:

public class MyServiceClass
{
    private readonly Task task;
    private HostControl hostControl;

    public MyServiceClass()
    {
        task = new Task(DoWork);
    }

    private void DoWork()
    {
        Console.WriteLine("Listen very carefully, I shall say this only once");
        hostControl.Stop();
    }

    public void Start(HostControl hostControl)
    {
        // so we can stop the service at the end of the check
        this.hostControl = hostControl;

        // start the DoWork thread
        task.Start();
    }

    public void Stop()
    {

    }

};

和更新的Program.cs

class Program
{
    static void Main(string[] args)
    {

        HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<MyServiceClass>((serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(() => new MyServiceClass());
                serviceConfigurator.WhenStarted((myServiceClass, hostControl) => myServiceClass.Start(hostControl));
                serviceConfigurator.WhenStopped(myServiceClass => myServiceClass.Stop());
            });    /* compiler thinks there's a ")" missing from this line */

            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDisplayName("MyService");
            hostConfigurator.SetDescription("Does stuff.");
            hostConfigurator.SetServiceName("MyService");
            hostConfigurator.StartAutomatically();
            hostConfigurator.EnableShutdown();
        });

    }
};

但是,这不会编译。我的编译器建议&#34;)&#34;我的评论(或上面的代码中显示)缺失(或在其周围),但任何添加的右括号都会添加到错误列表中。

我觉得我关闭......有什么想法吗?

1 个答案:

答案 0 :(得分:5)

最终让这个工作:

public class MyServiceClass : ServiceControl
{
    private readonly Task task;
    private HostControl hostControl;

    public MyServiceClass()
    {
        task = new Task(DoWork);
    }

    private void DoWork()
    {
        Console.WriteLine("Listen very carefully, I shall say this only once");
        //hostControl.Stop();
    }

    public bool Start(HostControl hostControl)
    {
        // so we can stop the service at the end of the check
        this.hostControl = hostControl;

        // start the DoWork thread
        task.Start();

        return true;
    }

    public bool Stop(HostControl hostControl)
    {
        return true;
    }

};

class Program
{
    static void Main(string[] args)
    {

        HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<MyServiceClass>(serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(() => new MyServiceClass());
                serviceConfigurator.WhenStarted((myServiceClass, hostControl) => myServiceClass.Start(hostControl));
                serviceConfigurator.WhenStopped((myServiceClass, hostControl) => myServiceClass.Stop(hostControl));
            });

            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDisplayName("MyService");
            hostConfigurator.SetDescription("Does stuff.");
            hostConfigurator.SetServiceName("MyService");
            hostConfigurator.StartAutomatically();
            hostConfigurator.EnableShutdown();
        });

    }
};

我的第二次尝试有一个备用&#34;(&#34;在第一次提到serviceConfigurator时,我需要将我的void Start和Stop函数转换为bool函数。希望这有助于某人。