You change the Main method;
static partial class Program { static void Main(string[] args) { RunAsService(); } static void RunAsService() { ServiceBase[] servicesToRun; servicesToRun = new ServiceBase[] { new MainService() }; ServiceBase.Run(servicesToRun); } }
And you create a new Windows service (MainService) and an installer class (MyServiceInstaller);
MainService.cs;
partial class MainService : ServiceBase { public MainService() { InitializeComponent(); } protected override void OnStart(string[] args) { base.OnStart(args); } protected override void OnStop() { base.OnStop(); } protected override void OnShutdown() { base.OnShutdown(); } }
MyServiceInstaller.cs;
[RunInstaller(true)] public partial class SocketServiceInstaller : System.Configuration.Install.Installer { private ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller; public SocketServiceInstaller() { InitializeComponent(); processInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller(); processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServiceName = "My Service Name"; var serviceDescription = "This my service"; Installers.Add(serviceInstaller); Installers.Add(processInstaller); } }
source share