經過一週的研究並嘗試獲取會話更改處理後,我仍然無法獲得我期待的結果。我查看了所有可用的示例並徹底檢查了TopShelf源代碼。我的主類是如下:如何使用TopShelf處理服務會話更改
class Program
{
static void Main(string[] args)
{
//Always start out in our apps base directory
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
const string serviceName = "Service";
const string displayName = "Service";
const string description = "Provide assistance";
try
{
HostFactory.Run(x =>
{
x.UseCommonLogging();
x.UseNinject(new IocModule());
x.Service<ServService.Service.WinService>(sc =>
{
sc.WhenStarted((s, hostControl) => s.Start(hostControl));
sc.WhenStopped((s, hostControl) => s.Stop(hostControl));
sc.WhenSessionChanged((se, e, id) =>
{
se.SessionChange(e, id);
});
//sc.WhenSessionChanged((s, chg) => s.SessionChange(chg));
sc.ConstructUsingNinject();
});
x.EnableSessionChanged();
x.RunAsLocalSystem();
x.EnableServiceRecovery(r =>
{
r.RestartService(0);
});
x.StartAutomatically();
x.SetDescription(description);
x.SetDisplayName(displayName);
x.SetServiceName(serviceName);
這裏是我的服務類:
{
class WinService : ServiceControl
{
private CancellationTokenSource cancelSource;
private CancellationToken ct;
public ILog Log { get; private set; }
public WinService(ILog logger)
{
if (logger == null)
throw new ArgumentNullException(nameof(logger));
}
public void SessionChange(SessionChangedArguments chg)
{
Log.Info("Service session changed!!!!!!!!!!!");
}
//Starts service
public bool Start(HostControl hostControl)
{
Console.Writeline("STARTED!);
return true;
}
//Stops service
public bool Stop(HostControl hostControl)
{
cancelSource.Cancel();
return true;
}
}
每次我運行代碼,也不管我從TopShelf源代碼API WindowsServiceHost.cs
使默認值進行任何更改類被印刷,而不是它是
「[Topshelf]服務會話改變」
但根據我的代碼它應該打印「服務會話更改!!!!!!!!!!!」 isntead。這裏是我所指的Top Shelf Source Code,以及相關的作品從第217行開始。任何幫助都將不勝感激。