SignalR无法与MVC配合使用,但可以与Webform配合使用

时间:2019-07-19 10:48:13

标签: c# asp.net-mvc sockets signalr sqldependency

SignalR代码可在WebForm.aspx中使用,但不能与MVC项目一起使用    在mvc5 Visual Studio 2019中不起作用,未显示错误并且集线器未开始使用。  我尝试了所有可能的解决方案,但感到无法正常工作 我尝试了这种可能的解决方案,下面也给出了链接 https://www.youtube.com/watch?v=30m-7wpmbrc&has_verified=1

using SignalRArrayList.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;

namespace SignalRArrayList.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
          return View();
        }
        [WebMethod]
        public IEnumerable<Products> GetAllMessages()
        {

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(@"SELECT [id],[back1],[lay1] FROM [dbo].[OddsInsert]", connection))
                {
                    // Make sure the command object does not already have
                    // a notification object associated with it.
                    command.Notification = null;
                    SqlDependency.Start(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

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

                    using (var reader = command.ExecuteReader())
                        return reader.Cast<IDataRecord>()
                            .Select(x => new Products()
                            {
                                id = x.GetInt32(0),
                                back1 = x.GetString(1),
                                lay1 = x.GetString(2)
                            }).ToList();
                }
            }
        }
        private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            MyHub.SendMessages();
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            return View();
        }
    }
}

ABout.csHTML代码 我的查看代码在这里

 @{
        ViewBag.Title = "About";
    }

    <div>
        <table id="tbl"></table>
    </div>
@section scripts{
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.4.1.min.js"></script>
    <script src="/signalR/hubs"></script>
    <script type="text/javascript">

        $(function () {

            // Proxy created on the fly
            var job = $.connection.myHub;
            alert("jhchjgchjg")

            // Declare a function on the job hub so the server can invoke it
            job.client.updateMessages = function () {
                getData();
            };

            // Start the connection
            $.connection.hub.start();
            getData();
        });

        function getData() {
            var $tbl = $('#tbl');
            $.ajax({
                url: 'Home/GetAllMessages',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                type: "POST",
                success: function (data) {
                    debugger;
                    if (data.d.length > 0) {
                        var newdata = data.d;
                        $tbl.empty();
                        $tbl.append(' <tr><th>ID</th><th>Name</th><th>Last Executed Date</th><th>Status</th></tr>');
                        var rows = [];
                        for (var i = 0; i < newdata.length; i++) {
                            rows.push(' <tr><td>' + newdata[i].id + '</td><td>' + newdata[i].bacl1 + '</td><td>' + newdata[i].lay1 + '</td><td>' + newdata[i].QuantDecimal + '</td></tr>');
                        }
                        $tbl.append(rows.join(''));
                    }
                }
            });
        }
    </script>
}

0 个答案:

没有答案