我最近发现了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和WhenStopped设置对象的属性时,Visual没有推断出正确的类型。我刚开始使用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");
});
}
}
}发布于 2014-09-19 11:59:20
虽然VisualStudio的intellisense没有推断出正确的类型,但它仍然应该编译。我不知道Top大陆架在做什么,但我记得上一次尝试使用它时,我遇到过这些问题。
发布于 2014-09-17 21:51:49
当您想要“注册”要在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。
https://stackoverflow.com/questions/25852846
复制相似问题