如何开始使用TopShelf

本文关键字:TopShelf 开始 何开始 | 更新日期: 2023-09-27 18:22:06

我最近发现了TopShelf。从我读到的所有东西来看,它看起来很酷。唯一的问题是我没能用它。我一定错过了什么。下面是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
namespace TestTopShelf {
public class FooBar {
    public FooBar() {
    }
    public void Start() { }
    public void Stop() { }
}
public class Program {
    public static void Main() {
        HostFactory.Run(x => {
            x.Service<FooBar>( s => { 
            });
        });
    }
}
}

你可以看到它有点不完整。当我尝试为ConstructUsing、WhenStarted和WhenStoped设置对象的属性时,Visual Studio没有推断出正确的类型。我是lambda表达式的新手,甚至是TopShelf的新手,所以我不确定自己在做什么。

我正在使用TopShelf文档中的此页面来开始。它看起来很直,所以我不确定我错过了什么。


更新代码


using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
namespace KeithLink.Svc.Windows.OrderService2 {
class FooBar {
    public FooBar() { }
    public void Start() { }
    public void Stop() { }
}
class Program {
    static void Main(string[] args) {
        HostFactory.Run(x => {
            x.Service<FooBar>(s => {
                s.ConstructUsing(name => new OrderService());
                s.WhenStarted(os => os.Start());
                s.WhenStopped(os => os.Stop());
            });
            x.RunAsLocalSystem();
            x.SetDescription("some service description");
            x.SetServiceName("ServiceName");
            x.SetDisplayName("Service Display Name");
        });
    }
}
}

如何开始使用TopShelf

尽管VisualStudio的intellisense没有推断出正确的类型,但它仍然应该编译。我不知道topshelf在做什么,但我记得上次尝试使用它时遇到了这些问题。

当您想用TopShelf"注册"一个服务以在启动时运行时,您可以调用Service<T>.Run方法——这似乎已经开始了。在该方法中,将传递一个HostConfigurator对象,您可以告诉(配置)TopShelf有关您的服务。关于您的服务,您可以配置各种各样的东西,但您通常想告诉它如何实例化您的服务以及如何停止和启动它。您可以通过传递执行这些事情的"代码"来实现这一点。您可以使用lambda来实现这一点,例如:

public static void Main() {
    HostFactory.Run(x => {
        x.Service<FooBar>( s => { 
            s.ConstructUsing(name => new FooBar());
            s.WhenStarted(fb => fb.Start());
            s.WhenStopped(fb => fb.Stop());
        });
        x.RunAsLocalSystem(); // use the local system account to run as
        x.SetDescription("My Foobar Service");  // description seen in services control panel
        x.SetDisplayName("FooBar"); // friendly name seen in control panell
        x.SetServiceName("foobar"); // used with things like net stop and net start
    });
}

这段代码不一定是lambdas,你可以创建一些方法来实现这一点(举个例子,这可能更清楚):

    private static void Main(string[] args)
    {
        HostFactory.Run(x =>
        {
            x.Service<FooBar>(s =>
            {
                s.ConstructUsing(CreateService);
                s.WhenStarted(CallStart);
                s.WhenStopped(CallStop);
            });
            x.RunAsLocalSystem(); // use the local system account to run as
            x.SetDescription("My Foobar Service");  // description seen in services control panel
            x.SetDisplayName("FooBar"); // friendly name seen in control panell
            x.SetServiceName("foobar"); // used with things like net stop and net start
        });
    }
    private static void CallStop(FooBar fb)
    {
        fb.Stop();
    }
    private static void CallStart(FooBar fb)
    {
        fb.Start();
    }
    private static FooBar CreateService(HostSettings name)
    {
        return new FooBar();
    }

这或多或少是从FooBar类开始的,如果有更具体的内容,请更新您的问题。

使用这些命名方法,当TopShelf开始运行时(在调用HostFactory.Run之后),将调用CreateSearch方法,然后调用CallStart方法,当服务停止时,将调用您的CallStop