SignalR无法与ASP.net WebForm项目一起使用

时间:2017-05-17 13:01:03

标签: c# asp.net signalr

为什么在添加到ASP.net webForm项目时SignalR无法正常工作? 并且它与asp.net webform空项目工作正常!!!

在我的代码下面 ***** MyHub.cs ****

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace SignalR_WF_1
{
    public class MyHub : Hub
    {
        public static void Show()
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
            context.Clients.All.displayStatus();
        }
    }
}

******* Startup.cs **********

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(SignalR_WF_1.Startup))]

namespace SignalR_WF_1
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();

        }
    }
}

*********新门票类**********

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SignalR_WF_1
{
    public class NewTickets
    {
        public int NTNotification { get; set; }
    }
}

********** Index.aspx ***********

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SignalR_WF_1.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-3.1.1.min.js"></script>
    <script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
    <script src="/signalR/hubs"></script>

    <script type="text/javascript">

        $(function () {

            // Proxy created on the fly
            var job = $.connection.myHub;

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

            // Start the connection
            $.connection.hub.start();
            getData();
        });
        function getData() {
            var $tbl = $('#tbl');
            $.ajax({
                url: 'index.aspx/GetData',
                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>Notifications</th></tr>');
                        var rows = [];
                        for (var i = 0; i < newdata.length; i++) {
                            rows.push(' <tr><td>' + newdata[i].NTNotification + '</td></tr>');
                        }
                        $tbl.append(rows.join(''));
                    }
                }
            });
        }
    </script>
</head>

<body>
    <form id="form1" runat="server">
        <div>
            <table id="tbl"></table>
        </div>
    </form>
</body>
</html>

********** Index.aspx.cs ***********

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.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SignalR_WF_1
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetData();
        }


        [WebMethod]
        public static IEnumerable<NewTickets> GetData()
        {

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(@"SELECT * FROM [MyTickets]", connection))
                {
                    // Make sure the command object does not already have
                    // a notification object associated with it.
                    command.Notification = null;
                    SqlDependency.Start(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].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 NewTickets()
                            {
                                NTNotification = x.GetInt32(0),
                            }).ToList();
                }
            }
        }

        private static void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            MyHub.Show();
        }
    }
}
检查了

数据库的连接字符串,并且每件事都是O.K

请帮助!

0 个答案:

没有答案