2015-09-29 43 views
0

有一個項目的WebAPI,我想提供的數據每5分鐘左右到客戶端:我如何發送每5分鐘的數據的WebAPI

using System; 
using System.Web.Http; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Converters; 

namespace MyWebApi.Controllers 
{ 
    public class EventController : ApiController 
    { 
     public string Get() 
     { 
      var id = Guid.NewGuid(); 
      string result = JsonConvert.SerializeObject(id, new IsoDateTimeConverter()); 
      return result; 
     } 

    } 
} 
+0

有一個例子那麼,什麼是錯誤或問題! –

+0

WebAPI每隔5分鐘發送一次數據的方式 –

+3

你到目前爲止試過的東西!!你的問題沒有顯示你的問題的任何研究工作。如果你想從WebAPI發送數據到客戶端,請看[SignalR](http://signalr.net/) –

回答

-1

你爲什麼不使用C#計時器。您可以創建一個靜態對象,並設置時間間隔爲5分鐘。 這裏是我從http://www.dotnetperls.com/timer

public static class TimerExample 
{ 

    static Timer _timer; 
    static List<DateTime> _l; 
    public static List<DateTime> DateList 
    { 
     get 
     { 
      if (_l == null) 
      { 
       Start(); // Start the timer 
      } 
      return _l; 
     } 
    } 
    static void Start() 
    { 
     _l = new List<DateTime>(); 
     _timer = new Timer(300000); // Set up the timer for 5 mins 

     _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); 
     _timer.Enabled = true; 
    } 
    static void _timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     // Add event here 
    } 
}