C# 如何开始使用TopShelf

C# 如何开始使用TopShelf,c#,topshelf,C#,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

我最近发现了托普舍夫。从我所读到的一切来看,它看起来很酷。唯一的问题是我没能使用它。我一定错过了什么。下面是我的代码

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 => { 

            });
        });
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Topshelf;
命名空间TestTopShelf{
公共级FooBar{
公共食品{
}
public void Start(){}
公共无效停止(){}
}
公共课程{
公共静态void Main(){
HostFactory.Run(x=>{
x、 服务(s=>{
});
});
}
}
}
你可以看到它有点不完整。当我试图为ConstructUsing设置“s”对象的属性时,WhenStarted和WhenStopped Visual Studio没有推断出正确的类型。我不熟悉lambda表达式,甚至对TopShelf也比较新,所以我不确定自己在做什么

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


更新代码


使用Autofac;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Topshelf;
命名空间KeithLink.Svc.Windows.OrderService2{
福巴级{
公共FooBar(){}
public void Start(){}
公共无效停止(){}
}
班级计划{
静态void Main(字符串[]参数){
HostFactory.Run(x=>{
x、 服务(s=>{
s、 ConstructUsing(name=>neworderservice());
s、 开始时(os=>os.Start());
s、 停止时(os=>os.Stop());
});
x、 RunAsLocalSystem();
x、 SetDescription(“某些服务描述”);
x、 SetServiceName(“ServiceName”);
x、 SetDisplayName(“服务显示名称”);
});
}
}
}
当您想“注册”一个服务以在启动时使用TopShelf运行时,您可以调用
service.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
    });
}
publicstaticvoidmain(){
HostFactory.Run(x=>{
x、 服务(s=>{
s、 ConstructUsing(name=>newfoobar());
s、 开始时(fb=>fb.Start());
s、 停止时(fb=>fb.Stop());
});
x、 RunAsLocalSystem();//使用本地系统帐户作为
x、 SetDescription(“我的Foobar服务”);//服务控制面板中的描述
x、 SetDisplayName(“FooBar”);//在控制面板中可以看到友好的名称
x、 SetServiceName(“foobar”);//与净停止和净启动等功能一起使用
});
}
此代码不必是lambdas,您可以创建方法来完成此操作(例如,这可能更清楚):

private static void Main(字符串[]args)
{
HostFactory.Run(x=>
{
x、 服务(s=>
{
s、 构造使用(CreateService);
s、 启动时(CallStart);
s、 停止时(呼叫停止);
});
x、 RunAsLocalSystem();//使用本地系统帐户作为
x、 SetDescription(“我的Foobar服务”);//服务控制面板中的描述
x、 SetDisplayName(“FooBar”);//在控制面板中可以看到友好的名称
x、 SetServiceName(“foobar”);//与净停止和净启动等功能一起使用
});
}
专用静态无效呼叫停止(FooBar fb)
{
fb.停止();
}
专用静态无效调用启动(FooBar fb)
{
fb.Start();
}
专用静态FooBar CreateService(主机设置名称)
{
返回新的FooBar();
}
这将在
FooBar
课程开始时为您提供最基本的信息,如果有更具体的信息,请更新您的问题


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

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

它是否没有按照你的预期工作?如果是这样的话,你认为它应该如何工作?它实际上是如何工作的?有错误吗?如果是,它们是什么?尽管VisualStudio的intellisense无法推断正确的类型,但它仍应编译。我不知道topshelf在做什么,但我记得上次尝试使用它时出现了这些问题。@PeterRitchie当我声明s时,它给我一条消息,在其他7个错误中说“委托”System.Func“不接受1个参数”。这很奇怪,因为当我尝试您发布的代码时,没有编译错误。这是当前NuGet包中的TopShelf 3.1.122。结果表明,一切都搞砸了,因为我调用的类没有用于启动和停止的公共方法。我将现有服务转换为类,并将方法声明为受保护。正确答案仍然来自@tobsen。一旦他创造了一个答案,我会欣然接受他的答案。这是一个怎样的答案?是的。在VisualStudio2015中尝试一下,自己看看。VisualStudio对此表示不满,但它仍在编译。
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
    });
}
    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();
    }