Signalr Cannot read Property 'Myhub' of Undefined

Asked

Viewed 90 times

0

Good afternoon, I am trying to apply the Signalr library on a Master page site in Webforms . aspx, but I am having error reading the Hub file. I can’t get the site to see such a file. I’ve run on a site without Masterpage and it works perfectly.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Myhub class:

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace ZaniADV
{
    [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 the 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();
}
}
  • Your code seems to be missing some things, like setting up the interface IAppBuilder, take a look at this link: https://stackoverflow.com/questions/18143599/can-signalr-be-used-with-asp-net-webforms

  • @Ricardopunctual already configured this way the interface Iappbuilder but nothing

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.