سؤال

I'm using Topshelf to host a Windows service. I am looking to get the hosted service to call to have itself restarted upon certain events. I was wondering how to achieve this?

Thanks, Ben

هل كانت مفيدة؟

المحلول

call Environment.Exit(1); when u want restart service

then in HostFactory add Enable ServiceRecovery

HostFactory.Run(configure =>
            {
                configure.Service((ServiceConfigurator<Service> service) =>
                {

                    service.WhenStarted(s => s.Start());
                    service.WhenStopped(s => s.Stop());
                });

                //Setup Account that window service use to run.  
                configure.RunAsNetworkService();
                configure.SetServiceName("ServiceName");
                configure.SetDisplayName("ServiceName");
                configure.SetDescription("Description");
                configure.StartAutomaticallyDelayed();
                configure.EnableServiceRecovery(recoveryOption =>
                {
                    recoveryOption.RestartService(0);
                });

            });

نصائح أخرى

You can use the service manager if you know the service name to call restart. It may or may not work called from itself. This isn't something Topshelf exposes, so you're on your own to do it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top