SqlDependency update works accurately in Microsoft edge but doesn't work properly in chrome or Firefox. What could be the cause for this kind of discrepancies?
Is there something I have to set up for different browsers to make SqlDependency update work the same way?
//I use webmethod called ProcessValue to communicate with ajax call in codebehind TestFolder/Test.aspx
[WebMethod]
publicstaticvoid ProcessValue(string value)
{
NotificationComponent.ProcessData(value.ToString());
}
//Then I have another class to handle the sqldependency onchange event as below
privatestaticstring result;
[WebMethod]
publicstaticvoid ProcessData(string value)
{
result = Convert.ToString(value);
}
publicstaticstring GetResult()
{
return result;
}
publicvoid RegisterNotification(DateTime currentTime)
{
string conStr = ConfigurationManager.ConnectionStrings["ChatConnection"].ConnectionString;
string sqlCommand = @"SELECT [ID],[MasterEmailID],[ChatToEmailID],[Message] from [dbo].[ChatPrivateMessageDetails] where [AddedOn] > @AddedOn";
using (SqlConnection con = new SqlConnection(conStr))
{
SqlCommand cmd = new SqlCommand(sqlCommand, con);
cmd.Parameters.AddWithValue("@AddedOn", currentTime);
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();
}
cmd.Notification = null;
SqlDependency sqlDep = new SqlDependency(cmd);
sqlDep.OnChange += sqlDep_Onchange;
using (SqlDataReader reader = cmd.ExecuteReader())
{
}
}
}
void sqlDep_Onchange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
SqlDependency sqlDep = sender as SqlDependency;
sqlDep.OnChange -= sqlDep_Onchange;
string Connectionid = GetResult();
var notificationHub = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
notificationHub.Clients.Client(Connectionid).notify("added");
RegisterNotification(DateTime.Now);
}
}
public List<ChatPrivateMessageDetail> GetContacts(DateTime afterDate)
{
using (Chat_PrivMsgDetail dc = new Chat_PrivMsgDetail())
{
return dc.ChatPrivateMessageDetails.Where(a => a.AddedOn > afterDate).OrderByDescending(a => a.AddedOn).ToList();
}
}
$div.find("#btnSendMessage").click(function () {
$textBox = $div.find("#txtPrivateMessage");
var msg = $textBox.val();
if (msg.length > 0) {
chatHub.server.sendPrivateMessage(userId, msg, 'Click');
$(document).ready(function () {
$.ajax({
type: 'POST',
url: '/TestFolder/Test.aspx/ProcessValue',
data: JSON.stringify({ value: userId }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
response;
},
error: function (error) {
error.responseText;
}
});
});
$textBox.val('');
$('#txtPrivateMessage').focus();
}
});