SignalR Can not read property 'MyHub' of undefined

0

Good afternoon, I'm trying to apply the SignalR library to a site with Master page in .aspx WebForms, but I'm getting error reading the Hub file. I can not get the site to see such a file. I've run into a website without MasterPage and it works perfectly.

MyHubclass:

usingMicrosoft.AspNet.SignalR;usingMicrosoft.AspNet.SignalR.Hubs;namespaceZaniADV{[HubName("MyHub")]
    public class MyHub : Hub
    {
        public static void Show()
        {
            IHubContext context = 
                GlobalHost.ConnectionManager.GetHubContext<MyHub>();
            context.Clients.All.displayStatus();
        }
    }
}

Startup class

using Microsoft.Owin;
using Owin;
using ZaniADV;

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

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

Global.asax

namespace ZaniADV
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            SqlDependency.Start(ConfigurationManager.ConnectionStrings
                ["MyConnectionString"].ConnectionString);
        }

        protected void Application_End()
        {
            SqlDependency.Stop(ConfigurationManager.ConnectionStrings
                ["MyConnectionString"].ConnectionString);
        }
    }

}

Javascript in Default.aspx page

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>Home</title>
<script src="../Scripts/jquery-3.1.1.min.js"></script>        
<script src="/ZaniADV/Scripts/jquery.signalR-2.2.1.min.js"></script>
<script src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>

        <script type="text/javascript">

            $(function () {
                var job = $.connection.MyHub;

                job.client.displayStatus = function () {
                    getData();
                };

                $.connection.hub.start();
                getData();
            });

            function getData() {
                var $tbl = $('#tbl');
                $.ajax({
                    url: 'Default.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>ComentarioID</th>
                <th>ComentarioTexto</th>
                <th>DataHoraComentario</th>
                <th>AdvogadoID</th><th>PostID</th><tr>');
                            var rows = [];
                            for (var i = 0; i < newdata.length; i++) {
                                rows.push(' <tr>
                <td>' + newdata[i].ComentarioID + 
                '</th><td>' + newdata[i].ComentarioTexto + 
                '</td><td>' + newdata[i].DataHoraComentario + 
                '</td><td>' + newdata[i].AdvogadoID + 
                '</tr><td>' + newdata[i].PostID + ' </tr><td>');
                            }
                            $tbl.append(rows.join(''));
                        }
                    }
                });
            }

        </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

        <div>
            <table id="tbl"></table>
        </div>
</asp:Content>

Code-Behind page Default.aspx

[WebMethod]
public static IEnumerable<Comentario> GetData()
{
    using (var connection = new SqlConnection
        (ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
    {
        connection.Open();
        using (var command = new SqlCommand(@"SELECT [ComentarioID], 
                [ComentarioTexto], [DataHoraComentario], [AdvogadoID], 
                [PostID] FROM [AdvogadoDB].[dbo].[Comentario]", connection))
        {
            command.Notification = null;

            SqlDependency.Start(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
            SqlDependency dependency = new SqlDependency(command);
            dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

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

            using (var reader = command.ExecuteReader())
                return reader.Cast<IDataRecord>()
                    .Select(x => new Comentario()
                    {
                        ComentarioID = x.GetInt32(0),
                        ComentarioTexto = x.GetString(1),
                        DataHoraComentario = x.GetDateTime(2),
                        AdvogadoID = x.GetInt32(3),
                        PostID = x.GetInt32(4),

                    }).ToList();
        }
    }
}

private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
    MyHub.Show();
}
}
    
asked by anonymous 10.01.2018 / 18:31

0 answers