我宁愿不轮询数据库,因为这会很浪费。我会通过只为我的数据(HTTP API 等)打开一个入口点来解决这个问题,然后通过 SignalR Hub 向所有连接的客户端广播更新。Brad Wilson 有一个非常酷的演示文稿,演示了这种方法:
Brad Wilson - Microsoft 的现代 Web 堆栈,由 ASP.NET Web API 主演
这是此方法的代码示例,它使用 ASP.NET Web API 技术进行数据输入。它使用内存字典进行数据存储,但这里不关心数据存储技术:
// This hub has no inbound APIs, since all inbound communication is done
// via the HTTP API. It's here for clients which want to get continuous
// notification of changes to the ToDo database.
[HubName("todo")]
public class ToDoHub : Hub { }
public abstract class ApiControllerWithHub<THub> : ApiController
where THub : IHub {
Lazy<IHubContext> hub = new Lazy<IHubContext>(
() => GlobalHost.ConnectionManager.GetHubContext<THub>()
);
protected IHubContext Hub {
get { return hub.Value; }
}
}
public class ToDoController : ApiControllerWithHub<ToDoHub> {
private static List<ToDoItem> db = new List<ToDoItem> {
new ToDoItem { ID = 0, Title = "Do a silly demo on-stage at NDC" },
new ToDoItem { ID = 1, Title = "Wash the car" },
new ToDoItem { ID = 2, Title = "Get a haircut", Finished = true }
};
private static int lastId = db.Max(tdi => tdi.ID);
// Lines removed for brevity
public HttpResponseMessage PostNewToDoItem(ToDoItem item) {
lock (db) {
// Add item to the "database"
item.ID = Interlocked.Increment(ref lastId);
db.Add(item);
// Notify the connected clients
Hub.Clients.addItem(item);
// Return the new item, inside a 201 response
var response = Request.CreateResponse(HttpStatusCode.Created, item);
string link = Url.Link("apiRoute", new { controller = "todo", id = item.ID });
response.Headers.Location = new Uri(link);
return response;
}
}
// Lines removed for brevity
}
Brad 演示的应用程序的完整源代码也可在:https ://github.com/bradwilson/ndc2012 获得。
您不喜欢的另一个选项是让您的数据库在数据更改后立即触发通知。然后,您可以将其拾取并通过 SignalR 进行广播。这是一个例子:
使用 SignalR 和 SqlDependency 的 ASP.NET 中的数据库更改通知