我遵循了这个guide,我确实设法让它发布我的数据库的值,但是当我更改数据库中的内容时它不会更新页面。
因此,当我调试NotificationHub.cs时,这两行会引发错误。
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
return context.Clients.All.RecieveNotification(totalNewMessages, totalNewCircles, totalNewJobs, totalNewNotification);
它抛出的错误是:
System.Core.dll中出现“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常,但未在用户代码中处理
附加信息:'System.Threading.Tasks.Task'类型不能隐式转换为'string'
以下是SendNotification代码:
public string SendNotifications()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
string query = "SELECT NotificationNumber FROM [dbo].[NotificationStatus] WHERE UserID=" + "1";
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
try {
command.Notification = null;
DataTable dt = new DataTable();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
if (dt.Rows.Count > 0)
{
totalNewMessages = Int16.Parse(dt.Rows[0]["NotificationNumber"].ToString());
}
connection.Close();
}
catch(Exception ex)
{
throw;
}
}
}
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
return context.Clients.All.RecieveNotification(totalNewMessages);
}
希望有人能指出什么是错的。
答案 0 :(得分:0)
context.Clients.All.RecieveNotification(totalNewMessages)
是一种动态方法,其返回类型为Task
;这不能转换为字符串,这是您尝试从SendNotifications返回的字符串。所以你需要改变签名,例如:
public Task SendNotifications();