I use TopShelf to host my Windows service. This is my installation code:
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(); }
I need to make sure that only one instance of my application can work at a time. Currently, you can run it as a Windows service and any number of console applications at the same time. If the application detects another instance during startup, it should exit.
I really like mutex , but don't know how to get this to work with TopShelf.
source share