2013-08-27 12 views
0

我在Global.asax上有以下代碼。它通常會產生電子郵件報警。我的問題是,我已經設置每24小時發送一封電子郵件,但代碼每10分鐘發送一次電子郵件。我已設置:自動電子郵件機制

HttpContext.Current.Cache.Add(CacheItemKey, "Test", null, 
       DateTime.MaxValue, TimeSpan.FromHours(24) 

但stll應用程序發送的電子郵件的每個10分鐘。我也嘗試了幾分鐘。

我的代碼:

private const string CacheItemKey = "CacheFromMe"; 

     public void CacheIRemovedCallback(string key, 
      object value, CacheItemRemoved reason) 
     { 


      HitmyPage(); 

      // Do the service works 

      DosomeWork(); 
     } 
     private const string myPageUrl = "myurl.aspx"; 

     private void HitmyPage() 
     { 

      WebClient myclient = new WebClient(); 
      myclient.DownloadData(myPageUrl); 
     } 
     protected void Application_BegintheRequests(Object sender, EventArgs e) 
     { 
      // If the dummy page is hit, then it means we want to add another item 

      // in cache 

      if (HttpContext.Current.Request.Url.ToString() == myPageUrl) 
      { 
       // Add the item in cache and when succesful, do the work. 

       RegistermyCacheEntries(); 
      } 
     } 
     private void DosomeWork() 
     { 

      DoEmailStuff(); 
      AnotherEmailStuff(); 

     } 
     private void DoEmailStuff() 
     { 

     // Statment For Sending The Email under conditions 



     } 
     private void AnotherEmailStuff() 
     { 
     // Another Statment for Sending Email 

     } 
void Application_Start(object sender, EventArgs e) 
     { 
      RegistermyCacheEntries(); 

     } 
     private bool RegistermyCacheEntries() 
     { 
      if (null != HttpContext.Current.Cache[CacheItemKey]) return false; 

      HttpContext.Current.Cache.Add(CacheItemKey, "Test", null, 
       DateTime.MaxValue, TimeSpan.FromHours(24), 
       CacheItemPriority.Normal, 
       new CacheIRemovedCallback(CacheIRemovedCallback)); 

      return true; 
     } 
+0

你試過24d嗎? –

+0

我已經完成了你對會議記錄所說的話。我有1440分鐘,每10分鐘發一次。我將其更改爲207360分鐘,並且每隔10分鐘發送一次,並且每20分鐘發送一次。沒有人錯誤地訪問我從該代碼調用的虛擬Url。唯一有權訪問的應用程序僅爲應用程序 – focus

+0

「CacheIRemovedCallback」中'reason'參數的值是什麼? –

回答

1

試試這個:

HttpContext.Current.Cache.Add(CacheItemKey, "Test", null, 
       DateTime.Now.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration, 
       CacheItemPriority.Normal, 
       new CacheIRemovedCallback(CacheIRemovedCallback)); 

Syntax as in MSDN:

public Object Add(
    string key, 
    Object value, 
    CacheDependency dependencies, 
    DateTime absoluteExpiration, 
    TimeSpan slidingExpiration, 
    CacheItemPriority priority, 
    CacheItemRemovedCallback onRemoveCallback 
) 

項目,保證留在緩存中至少10分鐘,這是一個默認設置。因此,每10分鐘發送一次電子郵件意味着設置到期時間不成功。你可以用absoluteExpiration和slidingExpiration參數來改變它。 但是我懷疑那個問題在別的地方。嘗試插入而不是 添加方法,如圖所示here

+0

重新開始每10分鐘發送一次電子郵件。不,它不工作 – focus

+0

我已經改變它插入,並仍然發送每10分鐘的電子郵件。我使用的代碼是: HttpContext.Current.Cache.Insert(CacheItemKey,「Test」,null, DateTime.Now.AddHours(24),System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, new CacheIRemovedCallback(CacheIRemovedCallback)); – focus