1

我正在尝试使用 .Net 中的 TopShelf 和 FluentScheduler 在 Windows 服务中每 10 秒触发一次事件,但我不只是每 10 秒触发一次事件。我正在分享我的实现,请指导我。

class Program
    {        
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                x.Service<IWindowsService>(s =>
                {
                    s.ConstructUsing(name => new WindowsService(new SchedulerRegistry(new Worker())));
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });

                x.RunAsLocalSystem();

                x.SetDescription("Test");
                x.SetDisplayName("Test Service");
                x.SetServiceName("Testservice");

                x.StartAutomatically();

                x.EnableServiceRecovery(s =>
                {
                    s.RestartService(1);
                    s.RestartService(2);
                });
            });
        }
    }

    public class SchedulerRegistry : Registry
    {
        public SchedulerRegistry(Worker worker)
        {
            Schedule(() =>
            {
                try
                {
                    worker.Run();
                }
                catch (Exception ex)
                {

                    throw;
                }
            }).NonReentrant().ToRunNow().AndEvery(10).Seconds();
        }
    }

    public interface IWindowsService
    {
        void Start();
        void Stop();
    }

    public class WindowsService : IWindowsService
    {
        public WindowsService(SchedulerRegistry registry)
        {
            JobManager.Initialize(registry);
        }

        public void Start()
        {
            Console.WriteLine("Service started");
        }

        public void Stop()
        {
            Console.WriteLine("Service stopped");
        }
    }

    public class Worker
    {
        public void Run()
        {
            CheckUrl();
        }

        public static void CheckUrl()
        {
            HttpWebResponse response = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://google.com");
                request.Method = "GET";

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                }
                else
                {
                }
            }
            catch (WebException e)
            {
                response.Close();
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }

    }
4

1 回答 1

0

通过执行以下操作,我能够让它运行:

public class WindowsService : IWindowsService
{
    private SchedulerRegistry registry;

    public WindowsService(SchedulerRegistry schedulerRegistry)
    {
        this.registry = schedulerRegistry;  
    }

    public void Start()
    {
        Console.WriteLine("Service started");

        JobManager.Initialize(this.registry);
    }

    public void Stop()
    {
        Console.WriteLine("Service stopped");
    }
}

基本上我所做的就是改变JobManager.Initialize(this.registry)被调用的位置。我认为您不希望它在您的服务启动之前运行,所以我认为这是有道理的。

于 2016-04-02T15:33:57.470 回答