[英]How to get started with TopShelf
我最近發現了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設置's'對象的屬性時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");
});
}
}
}
雖然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
});
}
這段代碼不一定是lambda,你可以創建方法來做這個(例如,這可能更清楚):
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.