在ASP.NET Web API中实现实时更新功能时遇到了一些困难。基本上,我网页的主页从API提取数据,该API是我主页的url扩展名。因此,例如,mywebpage.com是从mywebpage.com/api/data中提取的。我现在才意识到,这些数据仅在加载网页或刷新后才加载,因此我将数据从API提取到客户端的频率也无关紧要,因为API仅在初始加载时刷新数据或刷新。我相信轮询此API并异步检查更改会很容易,但是如果此API不是最新的,那甚至都没有关系。如何做到这一点,以便使该API保持最新状态,并可以从客户端中提取最新数据?该API从我的SQL数据库获取数据。
这是到目前为止我要从SQL获取数据的代码...我的最终希望是,有一种方法可以在OnChange事件触发时(也称为RefreshPage方法)刷新API中的数据。 ...
public List<TestStation> GetAllStations(string serverName)
{
List<TestStation> result = new List<TestStation>();
SqlDependency.Start("Data Source="
+ serverName + ";"
+ "Initial Catalog=...;"
+ "User Id=...;Password=...;");
using (SqlConnection connect = new SqlConnection("Data Source="
+ serverName + ";"
+ "Initial Catalog=...;"
+ "User Id=...;Password=...;"))
{
connect.Open();
//create SqlQuery to receive all station information
SqlCommand command = connect.CreateCommand();
string query = " SELECT * "
+ " FROM db_owner.Stations"
+ " ORDER BY StationName ASC;";
command.CommandText = query;
command.CommandTimeout = TIMEOUT_HEARTBEAT;
//need to use a second query for checking status with SqlDependency because SqlDependency does not allow queries with "Select * "
SqlCommand statusCommand = connect.CreateCommand();
string statusQuery = " SELECT PcName, TimeUtc, UtcOffset,"
+ " SchedulerVersion, StatusCode, StatusMessage "
+ " FROM db_owner.Heartbeat"
+ " WHERE PcName = @PCNAME"
// + " AND SchedulerVersion NOT LIKE 'DEV%' "
+ " ORDER BY TimeUtc DESC ;";
statusCommand.CommandText = statusQuery;
statusCommand.CommandTimeout = TIMEOUT_INSTRUCTIONS;
SqlDependency dependency =
new SqlDependency(statusCommand);
dependency.OnChange += new OnChangeEventHandler(RefreshPage);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
try
{
string name, Os, NPI, lastRestart, DutId, Suite, IDN, DUT_FQDN, CPU, Location;
bool suspended;
//name = reader.GetString(0);
name = (string)reader["StationName"];
List<string> updateInfo = GetStationUpdate("sepweb.srs.is.keysight.com", "SAD_NIGHTLY", name);
DutId = updateInfo[1];
Suite = updateInfo[2];
IDN = updateInfo[4];
DUT_FQDN = updateInfo[3];
CPU = updateInfo[5];
Location = updateInfo[6];
Os = (string)reader["OS"];
// Get NPI from the Profile Table
NPI = GetStationNPI(name, serverName);
if (!reader.IsDBNull(3))
{
lastRestart = (string)reader["LastRestart"]; ;
}
else
{
lastRestart = "01/01/0001 1:00:00 AM";
}
suspended = (bool)reader["Suspended"];
// Get Status From the Heartbeat Table
string status = RunningStatus(name, serverName);
// Create a test station object and add it to the list
result.Add(new TestStation(name, Os, NPI, lastRestart, suspended, status, DutId, IDN, DUT_FQDN, CPU, Suite, Location));
}
catch
{
continue;
}
}
}
return result;
}
void RefreshPage(object sender, SqlNotificationEventArgs e)
{
;
}
此外,我使用此代码的Web API控制器如下所示。
public class ValuesController : ApiController
{
// GET api/values
public List<TestStation> GetStations()
{
SQLQuery sqlQ = new SQLQuery();
List<TestStation> stationList = sqlQ.GetAllStations("...website.com");
return stationList;
}