2017-06-05 1 views
0

ich ein HTTP-Modul implementiert haben, dieAusführen eines Asynchron-Methode nach jeweils n Minuten

using System.Web; 
using System.Threading.Tasks; 
using System; 
using System.Net.Http; 

namespace BL.HttpModules 
{ 
    public class MyCustomAsyncModule : IHttpModule 
    { 
     #region Static Privates 

     private static bool applicationStarted = false; 
     private readonly static object applicationStartLock = new object(); 

     #endregion 

     public void Dispose() 
     { 

     } 

     /// <summary> 
     /// Initializes the specified module. 
     /// </summary> 
     /// <param name="httpApplication">The application context that instantiated and will be running this module.</param> 
     public void Init(HttpApplication httpApplication) 
     { 
      if (!applicationStarted) 
      { 
       lock (applicationStartLock) 
       { 
        if (!applicationStarted) 
        { 
         // this will run only once per application start 
         this.OnStart(httpApplication); 
        } 
       } 
      } 
      // this will run on every HttpApplication initialization in the application pool 
      this.OnInit(httpApplication); 
     } 

     public virtual void OnStart(HttpApplication httpApplication) 
     {    
      httpApplication.AddOnBeginRequestAsync(OnBegin, OnEnd); 
     } 

     private IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extraData) 
     { 
      applicationStarted = true; 
      var tcs = new TaskCompletionSource<object>(extraData); 
      DoAsyncWork(HttpContext.Current).ContinueWith(t => 
      { 
       if (t.IsFaulted) 
       { 
        tcs.SetException(t.Exception.InnerExceptions); 
       } 
       else 
       { 
        tcs.SetResult(null); 
       } 
       if (cb != null) cb(tcs.Task); 
      }); 
      return tcs.Task; 
     } 

     async Task DoAsyncWork(HttpContext ctx) 
     { 
      var client = new HttpClient(); 
      var result = await client.GetStringAsync("http://google.com"); 
      // USE RESULT 
     } 

     private void OnEnd(IAsyncResult ar) 
     { 
      Task t = (Task)ar; 
      t.Wait(); 
     } 

     /// <summary>Initializes any data/resources on HTTP module start.</summary> 
     /// <param name="httpApplication">The application context that instantiated and will be running this module.</param> 
     public virtual void OnInit(HttpApplication httpApplication) 
     { 
      // put your module initialization code here 


     } 

    }// end class 
}// end namespace 

I DoAsyncWork nach jeweils 5 Minuten feuern wollen auf Anfrage Beginn meiner ASP.NET-Anwendung gefeuert wird. Können Sie mir helfen, dieses Ziel in diesem Modul zu erreichen?

+0

Sie wissen, dass geplante Aufgaben aller Art in asp.net ist eine schlechte Idee, nicht wahr? –

+0

Mögliches Duplikat von [Async-Methode regelmäßig mit festgelegtem Intervall ausführen] (https://stackoverflow.com/questions/30462079/run-async-method-reglically-with-specified-interval) – Amy

+0

@Amy Ich würde mit dieser spezifischen Duplik nicht einverstanden sein Die Tatsache, dass ASP.NET die Einschränkungen für wiederholte Arbeitsschritte ändert, führt dazu, dass beide Lösungen im verknüpften Duplikat aufgrund des Wiederverwendungspools nicht langfristig auf ASP.NET funktionieren. –

Antwort

0

Es gibt keinen integrierten Weg in IIS, um dies zuverlässig zu tun, müssen Sie einen externen Prozess oder eine Drittanbieter-Bibliothek verwenden, um die auszuführende Arbeit zu planen. Hangfire ist eine sehr beliebte Bibliothek, mit der Sie sowohl geplante als auch nicht geplante Tasks ausführen können.

Verwandte Themen