用ASP.NET模拟Windows Service来实现定时提醒之类的功能

Windows的计划任务大家应该都熟悉,但是怎样在Web上实现类似的功能呢?调用Windows Service不太现实,因为很多时候我们不能去设置服务器自身的环境。

那么我们来考虑如何在Web这种无状态的环境下模拟同样的功能呢?首先我们需要一个回发的事件,这样才能触发我们所要的效果。那么这里有几种方案可以选择:

1、一个页面被请求的时候
2、应用程序的开始和结束
3、一个Session的开始、结束或超时
4、缓存过期

前3种基本不可用,具体原因就不多解释了(b/s的应该都明白),所以只能用第四种。其实主要原理是利用Cache类中的方法
public void Insert ( System.String key , System.Object value ,
                     System.Web.Caching.CacheDependency dependencies ,
                     System.DateTime absoluteExpiration ,
                     System.TimeSpan slidingExpiration ,
                     System.Web.Caching.CacheItemPriority priority ,
                     System.Web.Caching.CacheItemRemovedCallback onRemoveCallback )
的最后一个参数,他是一个delegate。

下面是具体实现:

首先我们需要在应用程序开始的时候给程序中注入一个模拟的Cache:
None.gif private   const   string  DummyCacheItemKey  =   " GagaGuguGigi " ;
None.gif
None.gif
protected   void  Application_Start(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    RegisterCacheEntry();
ExpandedBlockEnd.gif}

None.gif 
None.gif
private   bool  RegisterCacheEntry()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
ifnull != HttpContext.Current.Cache[ DummyCacheItemKey ] ) return false;
InBlock.gif 
InBlock.gif    HttpContext.Current.Cache.Add( DummyCacheItemKey, 
"Test"null
InBlock.gif        DateTime.MaxValue, TimeSpan.FromMinutes(
1), 
InBlock.gif        CacheItemPriority.Normal,
InBlock.gif        
new CacheItemRemovedCallback( CacheItemRemovedCallback ) );
InBlock.gif 
InBlock.gif    
return true;
ExpandedBlockEnd.gif}

这里需要注意一下,过期时间只能设为2分钟以上。如果你输入的时间小于2分钟,但仍然是以2分钟计算。(可能是.NET自身的问题)

这样会触发CacheItemRemovedCallback事件,CacheItemRemovedCallback事件的原型如下:

None.gif public   void  CacheItemRemovedCallback(  string  key, 
None.gif            
object  value, CacheItemRemovedReason reason)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
ExpandedBlockEnd.gif}


在这其中我们可以将我们要做的事情的代码添加进去。

------------------------------------  分割 ------------------------------------

等等,不要以为这样就完了,这只是你第一次触发了这个事件。在这个事件完了之后,Cache已经被认定为过期,因为我们要想办法重新将新的Cache加进去。

我们的办法是模拟一次页面请求,然后在这次请求里将Cache添加进去:

上面的Callback事件中加入

None.gif public   void  CacheItemRemovedCallback(  string  key, 
None.gif            
object  value, CacheItemRemovedReason reason)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Debug.WriteLine(
"Cache item callback: " + DateTime.Now.ToString() );
InBlock.gifInBlock.gif    HitPage();
InBlock.gif
InBlock.gif    
// Do the service works
InBlock.gif
    DoWork();
ExpandedBlockEnd.gif}

DoWork()表示你所要做的事情。

HitPage我们这样定义:

None.gif private   const   string  DummyPageUrl  =  
None.gif    http://localhost/Reminder/WebForm1.aspx
;
None.gif
None.gif
private   void  HitPage()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    WebClient client 
= new WebClient();
InBlock.gif    client.DownloadData(DummyPageUrl);
ExpandedBlockEnd.gif}

模拟页面在执行的时候我们通过Application_BeginRequest这个事件将Cache加入进去
None.gif protected   void  Application_BeginRequest(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// If the dummy page is hit, then it means we want to add another item
InBlock.gif    
// in cache
InBlock.gif
    if( HttpContext.Current.Request.Url.ToString() == DummyPageUrl )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
// Add the item in cache and when succesful, do the work.
InBlock.gif
       RegisterCacheEntry();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

这样,每次在WebForm1这个页面被请求的时候Cache便会被重新加入。

源码下载: http://files.cnblogs.com/gamix/Reminder.rar

文章来源:codeproject
地址: http://www.codeproject.com/aspnet/ASPNETService.asp#xx1277369xx
作者:Omar Al Zabir

转载于:https://www.cnblogs.com/gamix/archive/2005/11/19/280299.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值