Search code examples
.netwindows-servicesstartup

How to make windows service start automatically


I am trying to make a windows service to start automatically before you are in a session.
I have tried using TopShelf and adding the Start Automatically method, but the service does not get started when i start the computer.I still have to start it manually. Is there anything i am missing ?

Service Start

public static void RunService() {
            var exitCode = HostFactory.Run(x => {
                x.Service<SomeService>(s => {
                    s.ConstructUsing((h) => new SomeService());
                    s.WhenStarted(t => t.Start());
                    s.WhenStopped(t => t.Stop());
                    s.WhenSessionChanged((daemon, host, args) => daemon.SessionChanged(args.SessionId));
                });
                x.EnableSessionChanged();
                x.EnableShutdown();

                x.StartAutomatically();
                x.RunAsLocalSystem();

            });
            int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
        }

Solution

    1. Check the Services Control Panel application ("services.msc") and confirm that your service's startup type is "Automatic".
    2. Using the Event Viewer, check the Windows Logs > Application area for messages from your service. Perhaps it is starting at boot but crashing or stopping quickly.
    3. Using the Event Viewer, check the Windows Logs > System area of the event logs for errors starting your service. Review records where the source is "Service Control Manager".

    See "Why doesn’t my Windows Service Start at Boot?" for additional advice.