Hi Folks,
in my asp.net website I have signalR installed and when an exception is recorded in the Errors Table of my MS SQL Server the SqlDependency is run, the error is received from the db, the ErrorHub sends the message to all context.Clients.all....
My page shows a pnael with the message for 5 seconds. So far so good.
I then click on a menu choice. Change from default to another aspx page (with the same master page). If an error is set in the db, the SqlDependency works, the ErrorHub sends to context.Clients.All, but the panel is not shown anymore. How come?
What I have tried:
[HubName("errorHub")]
public sealed class ErrorMessages : Hub
{
private Dependencies.DependencyGenericHandler _dependency;
private DateTime _currentTime;
private ErrorMessages()
{
_currentTime = DateTime.Now;
_dependency = new Dependencies.DependencyGenericHandler("MVS.SQLDEP_Errors");
_dependency.ClearParameters();
_dependency.AddParameters("@ModifiedByUser", Accounts.AccountControl.CurrentUsername);
_dependency.AddParameters("@CurrentDateTime", _currentTime);
_dependency.DependencyResult += Dependency_DependencyResult;
}
public static readonly ErrorMessages Instance = new ErrorMessages();
public void CallDepency()
{
try
{
_dependency.DependencyResultTable();
}
catch (Exception err)
{
Logging.Log.AddException(err);
}
}
[HubMethodName("displayErrorMessage")]
public void Dependency_DependencyResult(DataTable resultTable)
{
try
{
_dependency.DependencyResult -= Dependency_DependencyResult;
StringBuilder sb = new StringBuilder();
Parallel.ForEach(resultTable.AsEnumerable(), row =>
{
sb.AppendLine($"{row.Field<string>("Errormessage")}{Environment.NewLine}");
});
var context = GlobalHost.ConnectionManager.GetHubContext<ErrorMessages>();
context.Clients.All.displayErrorMessage(sb.ToString());
_dependency.DependencyResult += Dependency_DependencyResult;
}
catch (Exception err)
{
Logging.Log.AddException(err);
}
}
}
My jQuery =
<script type="text/javascript">
$(function () {
var errorHub = $.connection.errorHub;
errorHub.client.displayErrorMessage = function (resultTable) {
try {
$('#<%=PnlErrors.ClientID %>').show();
$('#<%= LbLError.ClientID %>').text(resultTable);
setTimeout(" $('#<%=PnlErrors.ClientID %>').hide();", 5000);
}
catch (e) {
console.log(e);
}
}
});
$.connection.hub.start()
.done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })
.fail(function () { console.log('Could not Connect!'); });
</script>