2016-12-06 62 views
0

我使用HttpHandler構建了一個RESTful web服務來訪問MSSQL數據庫,我需要調度將由用戶操作觸發的方法調用,即用戶在下一個時間安排事件一週17點,所以負責發送通知的方法將在當時被解僱。我嘗試使用System.Threading.Timer下面的代碼,但它沒有工作如何在使用ASP.NET構建的web服務中調度方法調用

ServiceAPI s = new ServiceAPI(); 
//s.CreatePreferences(true, true, true, 3); 
TimeSpan alertTime = new TimeSpan(17, 30, 00); 
DateTime current = DateTime.Now; 
TimeSpan timeToGo = new TimeSpan(00, 01, 00);//alertTime - current.TimeOfDay; 
if(timeToGo < TimeSpan.Zero) 
{ 
     return; // time already passed 
} 
System.Threading.Timer timer = new System.Threading.Timer(x => 
{ 
     s.SendNotification(true, true, true, 10); 
}, null, timeToGo, System.Threading.Timeout.InfiniteTimeSpan); 

P.S.我的web服務託管在IIS服務器中。

回答

0

你不能從webservice項目中完成它,你需要一個客戶端,它將根據一些預定義的時間表調用你的webservice方法,你使用的是Windows Azure嗎? (如果有的話,那裏有一個調度程序功能:https://azure.microsoft.com/ro-ro/services/scheduler/)。

+0

不幸的是,它被託管在一個名爲somee的免費託管服務中。除了從客戶端觸發它之外,沒有其他辦法可以做到嗎?我的客戶端平臺在Android上,我正在使用Google的Firebase雲消息傳遞進行定期通知,但是我需要在計劃事件的情況下編寫推送通知,因此它必須從服務器端觸發,不要喲? – AymanKun