2016-03-28 5 views
0

.NETでTopShelfとFluentSchedulerを使用してWindowsサービスで10秒ごとにイベントをトリガーしようとしていますが、10秒ごとにイベントが発生するだけではありません。私は私の実装を共有しています、親切に私を導く。FluentSchedulerが動作しない

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(); 
       } 
      } 
     } 

    } 
+0

これは答えではありませんが、スケジューリングなどを行うことができるHangfire(http://hangfire.io)を試してみてください。それは非常に広範なユーザーベースと良い文書を持っています。 –

+0

それはまったく発射ですか? –

+0

@ColeWまったくありません – Anony

答えて

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)と呼ばれていた場所を変更することでした。私はあなたのサービスがとにかく始まるまでそれを実行したいとは思わないので、これは理にかなっていると思います。