I am new in SignalR .
my project is that bringing up sql change on signalR and sql dependency .
This is sample code that used C# corner
Every thing is ok but i get exception through this code
using (var connection = new SqlConnection("Server=.;Database=fidilio;Trusted_Connection=True;"))
{
const string query = "SELECT Count(*) FROM [dbo].[MemberComment]";
connection.Open();
using (var command = new SqlCommand(query, connection))
{
command.Notification = null;
var dt = new DataTable();
var dependency = new SqlDependency(command);
dependency.OnChange += dependency_OnChange;
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteScalar();
commentCount = Int16.Parse((reader.ToString()));
}
}
var context = GlobalHost.ConnectionManager.GetHubContext<NotficationHub>();
return context.Clients.All.RecevieNotification(commentCount);
Is there any idea ?
Unfortunately, the code you posted is incomplete and far from clear, never mind useful. Note, for example, that the code posted as actual code does not actually match the code you posted as a bitmap.
(Please also keep in mind that bitmaps, especially those for which the description still reads "enter image description here", cannot be searched in any meaningful way by tools like the web-site's own search feature or search engines like Bing and Google).
However, the specific exception you got is very typical of misuse of an async
method. It seems to me that you could get the code to work by changing the return statement to look like this:
return (string)context.Clients.All.RecevieNotification(commentCount).Result;
(That's the code from the text version of your question...copy/paste is also easier than trying to retype something from a bitmap. I presume you can adapt the above change to the code in the bitmap if needed).
Note that if that actually does fix the problem, then what you really should do is change the method containing that return statement to be async Task<string>
, and change the return statement to this:
return (string)(await context.Clients.All.RecevieNotification(commentCount));
But doing so will simply push the need to use await
back up to the calling method, and the caller of that method, and so on, until you get to whatever top-level method in the thread or UI event started the whole process.
That is really the right thing to do, but there's not enough context here to explain how to do that in any specific way. If you do decide to fix your code that way, and need help figuring out how to correctly make your code async
-aware, please post a new question, being sure to include a good, minimal, complete code example that clearly and reliably illustrates that question.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments