我的 Global.asax 上有以下代码。它正常产生电子邮件警报。我的问题是我已设置每 24 小时发送一封电子邮件,但代码每 10 分钟发送一次电子邮件。我已经设定 :
HttpContext.Current.Cache.Add(CacheItemKey, "Test", null,
DateTime.MaxValue, TimeSpan.FromHours(24)
但是应用程序每 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;
}