数据库变更SignalR实时反馈

Database change SignalR real time feedback

我完全搞不懂到底是什么问题。直到我刷新我的页面才更新记录....

代码==>>> 反馈资料库

  public class FeedbackRepository
    {

        readonly string _connString =
        ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        public IEnumerable<Feedback> GetAllMessages()
        {
            var messages = new List<Feedback>();
            using (var connection = new SqlConnection(_connString))
            {
                connection.Open();
                using (var command = new SqlCommand(@"SELECT [FeedbackID], 
                [email], [subject], [message] FROM [dbo].[Feedbacks]", connection))
                {
                    command.Notification = null;

                    var dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {



                        messages.Add(item: new Feedback
                        {
                            FeedbackID = (int)reader["FeedbackID"],

                            email = (string)reader["email"],
                            subject = reader["subject"] != DBNull.Value ?
                        (string)reader["subject"] : "",
                            message =(string) reader["message"]
                        });
                    }
                }
            }

            return messages;
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                FeedbackHub.SendMessages();
            }
        }

    }

这是我的 table 架构 ...

反馈

 public class Feedback
    {
        [Required]
        [Key]
        public int FeedbackID { get; set; }

        public string email { get; set; }
        [Required]
        public string subject { get; set; }
        [Required]
        public string message { get; set; }
    }

这是我的控制器代码

   public ActionResult GetFeedback()
        {
            FeedbackRepository _feedbackRepository = new FeedbackRepository();


            return PartialView("_feedbackList", _feedbackRepository.GetAllMessages());
        }

这是局部视图==>

_feedbacksList

 <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.subject)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.message)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.subject)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.message)
                </td>
                <td>

                    @Html.ActionLink("Delete", "Delete", new { id = item.email })
                </td>
            </tr>
        }

    </table>

这是我的反馈中心代码... feedbackHUb

public class 反馈中心:中心 { 私有静态字符串 conString = ConfigurationManager.ConnectionStrings["conn"].ToString(); public无效你好() { Clients.All.hello(); }

[HubMethodName("sendMessages")]
public static void SendMessages()
{
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FeedbackHub>();
    context.Clients.All.updateMessages();
}

} 在这里查看代码==>>>

<div id="messagesTable"></div>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="/signalr/hubs"></script>

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var notifications = $.connection.messagesHub;

        //debugger;
        // Create a function that the hub can call to broadcast messages.
        notifications.client.updateMessages = function () {
            getAllMessages()

        };
        // Start the connection.
        $.connection.hub.start().done(function () {
            alert("connection started")
            getAllMessages();
        }).fail(function (e) {
            alert(e);
        });
    });

    function getAllMessages()
    {
        var tbl = $('#messagesTable');
        $.ajax({
            url: '/Home/GetFeedback',
            contentType: 'application/html ; charset:utf-8',
            type: 'GET',
            dataType: 'html'
        }).success(function (result) {
            alert("connection started")
            tbl.empty().append(result);
        }).error(function () {

        });
    }
</script>

我还在 startup.cs class 和 Global.asax 文件中添加了必要的代码。

注意:**我已经启用了**服务代理:...

我不知道到底是什么问题...

我认为你的问题出在你的视图代码中......

 var notifications = $.connection.messagesHub;

messageHub 不是您的集线器,您应该将其替换为您自己的集线器,如下所示

var notifications = $.connection.feedbackHub;

希望这会解决你的问题