我正在使用 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();
}
我需要确保我的应用程序只有一个实例可以同时运行。目前,您可以将其作为 Windows 服务和任意数量的控制台应用程序同时启动。如果应用程序在启动期间检测到其他实例,它应该退出。
我真的很喜欢基于互斥锁的方法,但不知道如何使用 TopShelf。