强制使用TopShelf服务的单个实例

Top*_*erz 4 c# windows-services single-instance topshelf

我正在使用TopShelf来托管我的Windows服务.这是我的设置代码:

static void Main(string[] args)
{
    var host = HostFactory.New(x =>
    {
        x.Service<MyService>(s =>
        {
            s.ConstructUsing(name => new MyService());
            s.WhenStarted(tc => tc.Start());
            s.WhenStopped(tc => tc.Stop());
        });

        x.RunAsLocalSystem();
        x.SetDescription(STR_ServiceDescription);
        x.SetDisplayName(STR_ServiceDisplayName);
        x.SetServiceName(STR_ServiceName);
    });

    host.Run();
}
Run Code Online (Sandbox Code Playgroud)

我需要确保只有一个应用程序实例可以同时运行.目前,您可以同时将其作为Windows服务和任意数量的控制台应用程序启动.如果应用程序在启动期间检测到其他实

我真的很喜欢基于互斥的方法,但不知道如何使用TopShelf.

Top*_*erz 5

这对我有用.事实证明它非常简单 - 互斥代码仅存在于控制台app的Main方法中.以前我用这种方法进行了假阴性测试,因为我在互斥锁名称中没有'全局'前缀.

private static Mutex mutex = new Mutex(true, @"Global\{my-guid-here}");

static void Main(string[] args)
{
    if (mutex.WaitOne(TimeSpan.Zero, true))
    {
        try
        {
            var host = HostFactory.New(x =>
            {
                x.Service<MyService>(s =>
                {
                    s.ConstructUsing(name => new MyService());
                    s.WhenStarted(tc =>
                    {
                        tc.Start();
                    });
                    s.WhenStopped(tc => tc.Stop());
                });
                x.RunAsLocalSystem();
                x.SetDescription(STR_ServiceDescription);
                x.SetDisplayName(STR_ServiceDisplayName);
                x.SetServiceName(STR_ServiceName);
            });

            host.Run();
        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }
    else
    {
        // logger.Fatal("Already running MyService application detected! - Application must quit");
    }
}
Run Code Online (Sandbox Code Playgroud)