如何使用 TopShelf 处理服务会话更改

How to handle service session changes with TopShelf

经过一周的研究并尝试处理会话更改后,我仍然无法获得我正在寻找的结果。我查看了所有可用示例并彻底检查了 TopShelf 源代码。我的主要class如下:

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:

{

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.csclass 中打印默认值,而不是

"[Topshelf] Service session changed"

但根据我的代码,它应该打印 "Service session changed!!!!!!!!!!!" isnead。这是我所指的 Top Shelf Source Code,相关文章从第 217 行开始。一如既往地非常感谢您的帮助。

一个老问题,但我能够使用我的 IMyService class(和具体的)触发我的事件,即 "standalone",也就是不从任何服务基础继承微软或 topshelf

public interface IMyServiceContract
{
    void Start();

    void Stop();

    void SessionChanged(Topshelf.SessionChangedArguments args);
}

我的具体:

public class MyService : IMyServiceContract
{

    public void Start()
    {
    }

    public void Stop()
    {

    }

    public void SessionChanged(SessionChangedArguments e)
    {
        Console.WriteLine(e.ReasonCode);
    }   

}

和我的 "program.cs "main"" 方法

            IMyServiceContract myServiceObject = new MyService(); // // container.Resolve<IMyServiceContract>();


            HostFactory.Run(x =>
            {
                x.Service<IMyServiceContract>(s =>
                {
                    s.ConstructUsing(name => myServiceObject);
                    s.WhenStarted(myso => myso.Start());
                    s.WhenStopped(myso => myso.Stop());
                    s.WhenSessionChanged((myso, hc, chg) => myso.SessionChanged(chg));
                });

                x.EnableSessionChanged();

如果您写入文本文件(我有 Console.WriteLine(e.ReasonCode);)...您可以看到更改。我通过执行 LockUser(在 windows 10 x64 中)并重新登录来进行测试。

对我有用。

我的 packages.config 所以你知道我使用的是哪个版本的 TopShelf

  <package id="log4net" version="2.0.5" targetFramework="net45" />
  <package id="Topshelf" version="4.0.3" targetFramework="net461" />
  <package id="Topshelf.Log4Net" version="4.0.3" targetFramework="net461" />