2012-06-06 137 views
1

我在Visual Studio Web Express中具有Web應用程序,並且在SQL Server Express中擁有db。 我想在每天下午5:00執行插入100條記錄daily.web應用程序是在asp.net MVC和vb.net中開發的。並使用IIS 7.5在服務器上部署。我應該遵循什麼邏輯?如何在Web應用程序中運行調度程序實用程序

回答

1

對我來說,我使用這種方法,它是很好至今:)

我用Ta SKS做,重新啓動在幾秒鐘這樣的時候了任務,這一次:

public enum ScheduledTasks 
{ 
    CleanGameRequests = 120, 
    AnotherTask = 30, 
} 

然後我開始我的Application_Start所有任務,以確保我的應用程序運行,而該任務將執行

protected void Application_Start() 
    { 
     ............... 
     // Add the tasks on Application starts 
     AddTask(ScheduledTasks.CleanGameRequests); 
     AddTask(ScheduledTasks.AnotherTask); 
    } 

OK,現在這裏是招:)

在AddTask方法

我只需要添加新的空項緩存,並根據任務的時間和呼叫該任務的合適的方法設置則absoluteExpiration它。

其實我的我無法解釋的思路很清晰,但這裏是代碼:

private static CacheItemRemovedCallback _onCacheRemove; 
    private void AddTask(ScheduledTasks task) 
    { 
     // Add my `CacheItemRemoved` method to be called on cache removed 
     _onCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved); 
     // Add new key to the cache with the name of this task 
     // and Expiration time acccordin to the task 
     HttpRuntime.Cache.Insert(task.ToString(), (int)task, null, 
      DateTime.Now.AddSeconds((int)task), Cache.NoSlidingExpiration, 
      CacheItemPriority.NotRemovable, _onCacheRemove); 
    } 

然後我的一切做的是選擇在CacheItemRemoved方法每個任務合適的方法:

public void CacheItemRemoved(string key, object time, CacheItemRemovedReason r) 
    { 
     //Get Task enum object 
     var task = (ScheduledTasks)Enum.Parse(typeof(ScheduledTasks), key); 
     // Select the suitable method to depending on the Task Enum object 
     switch (task) 
     { 
      case ScheduledTasks.CleanGameRequests: 
       GameRequest.CleanUp(); 
       break; 
      case ScheduledTasks.AnotherTask: 
       Service.AnotherTask(); 
       break; 
     } 
     // Don't forget to re-add the task to the cache to do it again and again 
     AddTask(task); 
    } 

最後一件事仍然是你的情況是檢查時間,如果是下午5點,我建議你把這個檢查在你的服務類。

希望這對你有所幫助:)

0

由於您使用的是sql server express版本,您不能在sql端創建預定作業。但你可以嘗試其他選項。

  1. Quartz.Net

  2. Service Broker approach

  3. 的Windows服務(如果你的主機提供商允許)

+0

好的,很好的選擇。但從邏輯上講,一旦我在服務器主機上託管/部署Web應用程序,並且正在寫實用程序方法並在系統時間上調用它,讓我們說下午5:00 –

+0

您可以通過Quartz.Net來實現,它可以幫助您創建可以編寫所有代碼的作業類和方法。它也有多種選項來控制預定的時間和更多。 – VJAI

+0

謝謝馬克,我認爲這是最好的方式,但是,它的簡單的自動功能調用發生在下午5:00。並且Web應用程序託管在db存在的服務器上。 –

相關問題