Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据登录的用户显示新邮件计数_C#_Asp.net_Signalr - Fatal编程技术网

C# 根据登录的用户显示新邮件计数

C# 根据登录的用户显示新邮件计数,c#,asp.net,signalr,C#,Asp.net,Signalr,目前正在开发一个用asp和c语言开发的网站。使用signalr,我已经成功地在每次数据库中收到新消息时发送新通知。然而,我现在遇到的问题是,假设User1有一条新消息,User2有两条新消息。它们都会显示给用户。因此,User1将有一条新消息,User2也将有一条新消息。我正在使用下面的代码 [HubMethodName("Notifications")] public string SendNotifications() { string userName =

目前正在开发一个用asp和c语言开发的网站。使用signalr,我已经成功地在每次数据库中收到新消息时发送新通知。然而,我现在遇到的问题是,假设User1有一条新消息,User2有两条新消息。它们都会显示给用户。因此,User1将有一条新消息,User2也将有一条新消息。我正在使用下面的代码

 [HubMethodName("Notifications")]
    public string SendNotifications()
    {
        string userName = HttpContext.Current.User.Identity.Name;

        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            string query = "SELECT  NewMessageCount FROM Messages WHERE UserName=@UserName";

            using (SqlCommand command = new SqlCommand(query, connection))
            {
                SqlParameter para2 = new SqlParameter(@"UserName", userName);
                command.Parameters.Add(para2);
                command.Notification = null;
                DataTable dt = new DataTable();
                SqlDependency dependency = new SqlDependency(command);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                connection.Open();

                SqlDataReader reader = command.ExecuteReader();
                dt.Load(reader);
                if (dt.Rows.Count > 0)
                {
                    totalNewMessages = Int16.Parse(dt.Rows[0]["NewMessageCount"].ToString());
                }
            }
        }
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
        return context.Clients.All.RecieveNotification(totalNewMessages);
    }

提前感谢你的帮助

是的,因为你正在向所有人广播。您尚未指定特定的连接。有关如何指定特定客户端/组的信息,请查看以下内容。 您应该使用Clients.userid

context.Clients.User(userName).RecieveNotification(totalNewMessages);