我尝试使用signalR来获取数据库发生变化时的通知。 但不起作用但没有错误。什么是我的错误代码。看起来像On方法dependency_OnChange无法正常工作
Global.asax中的我的代码用于启动&停止SqlDependency
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
//..
SqlDependency.Start(ConfigurationManager.ConnectionStrings["SQL_ConStr"].ConnectionString);
}
protected void Application_End()
{
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["SQL_ConStr"].ConnectionString);
}
这是我的班级观看有关变化的数据并获得返回数据
public DataTable DTResultOnSQLDepencyChange(string query)
{
Dt = new DataTable();
if (!String.IsNullOrEmpty(query))
{
OpenConn();
cmd = new SqlCommand(query, SqlConn);
cmd.Notification = null;
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (SqlConn.State == ConnectionState.Closed)
SqlConn.Open();
Da = new SqlDataAdapter(cmd);
Da.Fill(Dt);
}
return Dt;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
NotificationsHub.NotifyAllClients(e.Info.ToString());
}
这是我的中心
[HubMethodName("notifyAllClients")]
public static void NotifyAllClients(string message)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
context.Clients.All.displayNotification(message);
}
这是我的初创公司
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
app.MapSignalR();
}
}
这是我的控制器
[RoutePrefix("api")]
public class indexController : ApiController
{
[Route("index/{GetEvents}")]
public List<mdEvent> getEvents()
{
List<mdEvent> mdEvents = new List<mdEvent>();
string SQL = string.Format("select * from master");
MainDB mainDB = new MainDB();
DataTable dt = mainDB.DTResultOnSQLDepencyChange(SQL);
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
mdEvent _mdEvent = new mdEvent();
_mdEvent.main_master_id = dr["main_master_id"].ToString();
_mdEvent.event_id = dr["road_map_id"].ToString();
_mdEvent.road_map_date = dr["road_map_date"].ToString();
_mdEvent.road_map_time = dr["road_map_time"].ToString();
_mdEvent.num_passenger = dr["num_passenger"].ToString();
_mdEvent.base_vehicle_id = dr["base_vehicle_id"].ToString();
_mdEvent.description = dr["road_map_description"].ToString();
_mdEvent.from_location = dr["from_location"].ToString();
_mdEvent.to_location = dr["to_location"].ToString();
_mdEvent.duration = dr["duration"].ToString();
_mdEvent.requestor_id = dr["requestor_id"].ToString();
_mdEvent.requestor_name = dr["requestor_name"].ToString();
_mdEvent.requestor_position_id = dr["requestor_position_id"].ToString();
_mdEvent.requestor_position = dr["requestor_position"].ToString();
_mdEvent.requestor_dept_id = dr["requestor_dept_id"].ToString();
_mdEvent.master_description = dr["main_master_description"].ToString();
_mdEvent.remark = dr["remark"].ToString();
_mdEvent.request_type_id = dr["request_type_id"].ToString();
mdEvents.Add(_mdEvent);
}
}
return mdEvents;
}
}
这是我的观点
<script src="../Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>
<script>
function setupDatePicker() {
jQuery('#txt_start_date, #txt_end_date').datetimepicker({
format: 'Y-m-d H:i:s'
});
}
$(function () {
// Proxy created on the fly
var notify = $.connection.notificationsHub;
// Declare a function on the job hub so the server can invoke it
notify.client.displayNotification = function (messages) {
getEventsForCalendar(messages);
};
// Start the connection
$.connection.hub.start();
getEventsForCalendar();
});
function getEventsForCalendar(messages) {
if ((messages !== undefined) || (messages != "")) {
//console.log(messages)
}
var $tbl = $('#tblEventInfo');
$.ajax({
url: '../api/index/GetEvents',
type: 'GET',
datatype: 'json',
success: function (data) {
if (data.length > 0) {
$tbl.empty();
$tbl.append(' <tr><th>event_id</th><th>main_master_id</th><th>road_map_date</th><th>road_map_time</th>' +
'<th>num_passenger</th><th>base_vehicle_id</th><th>description</th><th>from_location</th>' +
'<th>to_location</th><th>duration</th><th>requestor_id</th><th>requestor_name</th>' +
'<th>requestor_position_id</th><th>requestor_position</th><th>requestor_dept_id</th><th>requestor_dept</th>' +
'<th>master_description</th><th>remark</th><th>request_type_id</th>' +
+'</tr>');
var rows = [];
for (var i = 0; i < data.length; i++) {
rows.push(' <tr><td>' + data[i].event_id + '</td><td>' + data[i].main_master_id + '</td>' +
'<td>' + data[i].road_map_date + '</td><td>' + data[i].road_map_time + '</td><td>' + data[i].num_passenger + '</td>' +
'<td>' + data[i].base_vehicle_id + '</td><td>' + data[i].description + '</td><td>' + data[i].from_location + '</td><td>' + data[i].to_location + '</td>' +
'<td>' + data[i].duration + '</td><td>' + data[i].requestor_id + '</td><td>' + data[i].requestor_name + '</td><td>' + data[i].requestor_position_id + '</td>' +
'<td>' + data[i].requestor_position + '</td><td>' + data[i].requestor_dept_id + '</td><td>' + data[i].requestor_dept + '</td><td>' + data[i].master_description + '</td>' +
'<td>' + data[i].remark + '</td><td>' + data[i].request_type_id + '</td>' +
'</tr>');
}
$tbl.append(rows.join(''));
}
}
});
}