试图停止托管topshelf服务

时间:2018-12-13 09:47:02

标签: c# service topshelf

我使用topshelf库进行服务,因此我希望在完成工作后将其停止,因此我使用了Environment.Exit(0);,这太可怕了 我想正常停止它,所以我搜索了很多次我都发现了

public bool Start(HostControl hostControl)
{
//your business logic
}
public bool Stop(HostControl hostCotnrol)
{
//your code to stop the service
}

他们让我发疯,我想要他的代码,我无法停止服务

  

那是我在程序课程中的主持人

class Program
{
    static void Main()
    {
        HostFactory.Run(serviceConfig =>
        {
            serviceConfig.Service<DataSendService>(serviceInstance =>
            {
                serviceConfig.UseNLog();

                serviceInstance.ConstructUsing(
                    () => new DataSendService());

                serviceInstance.WhenStarted(execute =>
                execute.OnStart());

                serviceInstance.WhenStopped(execute =>
                execute.OnStop());



            });

            serviceConfig.SetServiceName("MyService");
            serviceConfig.SetDisplayName("Service");
            serviceConfig.SetDescription("This service to sync the store prices");

            serviceConfig.EnableServiceRecovery(recover => recover.RestartService(1));
            serviceConfig.EnableServiceRecovery(recover => recover.RestartService(1));
            serviceConfig.EnableServiceRecovery(recover => recover.RestartService(1));


            serviceConfig.StartAutomaticallyDelayed();
            serviceConfig.RunAsLocalSystem();

        });
    }
}
  

那是我的启动功能

        public void OnStart()
    {

        Library.WriteErrorLog("windows service started");
        Log.InfoFormat($"{DateTime.Now}: Started");

        //clear selected colums list
        _query.SelectedColumns?.Clear();

        if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "AppConfig.xml"))
        {
            XmlConfigFile();
            Library.WriteErrorLog("xml file has been created");
        }

        ReadConfig(AppDomain.CurrentDomain.BaseDirectory + "AppConfig.xml");

        _dt = _query.DateTableBuilder();

        ExportToCsv(_dt, _query.StoreName);

        EmailSend();


    }

我发现了一个代码here帮助我安全地停止了服务,但没有启动我的启动功能中的功能 谢谢

0 个答案:

没有答案