2014-09-19 6 views
8

Devo implementare un'attività in background, qual è il mio compito? Ho una tabella che memorizza l'ammontare della locazione di ogni cliente, quindi ho bisogno di calcolare il prezzo di affitto in ogni mese dopo uno specifico datetime ackf così ho cercato su Google e ho trovato un pezzo di codice che (è nuget chiamato webbackgrounder) ho aggiunto alla mia soluzione e mi dà questa parte del codice per gestire il mio compito:Utilizzo di webbackgrounder nuget in MVC per eseguire attività in background per lungo tempo

using System; 
using System.Threading; 
using System.Threading.Tasks; 

    namespace WebBackgrounder.DemoWeb 
    { 
     public class SampleJob : Job 
     { 
      public SampleJob(TimeSpan interval, TimeSpan timeout) 
       : base("Sample Job", interval, timeout) 
      { 
      } 

      public override Task Execute() 
      { 
       return new Task(() => Thread.Sleep(3000)); 
      } 
     } 
    } 

Vorrei sapere come posso programmare il mio compito?

Maggiori dettagli: Here

ho trovato this article ma in realtà non so posso usare questo metodo per molto tempo ?? Cordiali saluti.

tutte le idee saranno apprezzate.

risposta

15

È inoltre necessario aggiungere una classe alla cartella App_Start dell'applicazione che avvia il processo e ne gestisce la durata. Si può vedere un esempio qui ... https://github.com/NuGet/WebBackgrounder/tree/master/src/WebBackgrounder.DemoWeb

ecco il codice dall'applicazione demo

using System; 
using Elmah; 
using WebBackgrounder.Jobs; 

[assembly: WebActivator.PostApplicationStartMethod(typeof(WebBackgrounder.DemoWeb.App_Start.WebBackgrounderSetup), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethod(typeof(WebBackgrounder.DemoWeb.App_Start.WebBackgrounderSetup), "Shutdown")] 

namespace WebBackgrounder.DemoWeb.App_Start 
{ 
    public static class WebBackgrounderSetup 
    { 
     static readonly JobManager _jobManager = CreateJobWorkersManager(); 

     public static void Start() 
     { 
      _jobManager.Start(); 
     } 

     public static void Shutdown() 
     { 
      _jobManager.Dispose(); 
     } 

     private static JobManager CreateJobWorkersManager() 
     { 
      var jobs = new IJob[] 
      { 
       new SampleJob(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20)), 
       /* new ExceptionJob(TimeSpan.FromSeconds(15)), */ 
       new WorkItemCleanupJob(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5), new WorkItemsContext()) 
      }; 

      var coordinator = new WebFarmJobCoordinator(new EntityWorkItemRepository(() => new WorkItemsContext())); 
      var manager = new JobManager(jobs, coordinator); 
      manager.Fail(ex => Elmah.ErrorLog.GetDefault(null).Log(new Error(ex))); 
      return manager; 
     } 
    } 
} 

Tuttavia ho trovato più semplice di utilizzare solo le parti di Webbackgrounder che mi servivano come segue. Mettere questa classe nella cartella App_Start

using System; 
using BombaySapphireCds.Jobs; 
using Elmah; 

[assembly: WebActivator.PostApplicationStartMethod(typeof(BombaySapphireCds.App_Start.PodMonitorConfig), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethod(typeof(BombaySapphireCds.App_Start.PodMonitorConfig), "Shutdown")] 

namespace BombaySapphireCds.App_Start 
{ 
    public static class PodMonitorConfig 
    { 
     private static PodMonitorJob m_job; 

     public static void Start() 
     { 
      m_job = new PodMonitorJob(TimeSpan.FromSeconds(20)); 
     } 

     public static void Shutdown() 
     { 
      m_job.Dispose(); 
     } 
    } 
} 

e la classe per fare il lavoro vero e proprio ... (mettere questo ovunque vi piace)

using System; 
using System.Threading; 
using System.Threading.Tasks; 

namespace BombaySapphireCds.Jobs 
{ 
    public class PodMonitorJob : IDisposable 
    { 
     private CancellationTokenSource m_cancel; 
     private Task m_task; 
     private TimeSpan m_interval; 
     private bool m_running; 

     public PodMonitorJob(TimeSpan interval) 
     { 
      m_interval = interval; 
      m_running = true; 
      m_cancel = new CancellationTokenSource(); 
      m_task = Task.Run(() => TaskLoop(), m_cancel.Token); 
     } 

     private void TaskLoop() 
     { 
      while (m_running) 
      { 
       // 
       // Do monitoring work here. 
       // 

       Thread.Sleep(m_interval); 
      } 
     } 

     public void Dispose() 
     { 
      m_running = false; 

      if (m_cancel != null) 
      { 
       try 
       { 
        m_cancel.Cancel(); 
        m_cancel.Dispose(); 
       } 
       catch 
       { 
       } 
       finally 
       { 
        m_cancel = null; 
       } 
      } 
     } 
    } 
} 
3

Questo è diventato il nuovo standard per l'esecuzione di sfondo operazione su la rete. È un pacchetto NuGet e si chiama HangFire - https://github.com/HangfireIO/Hangfire. I compiti persistono anche al di là del riciclo di apppool.

+2

Richiede .net 4.5. – kristianp

Problemi correlati