SignalR cannot read property client of undefined

前端 未结 10 2263
醉话见心
醉话见心 2020-12-13 08:42

I\'m trying to add SignalR to my project (ASPNET MVC 4). But I can\'t make it work.

In the below image you can see the error I\'m receiving.

相关标签:
10条回答
  • 2020-12-13 08:58

    For those who tried to add the generated proxy file path in the bundle.

    Do not include the "~/signalr/hubs" in your BundleConfig.cs.

    You can have the JQuery.SignalR in the bundle:

    bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
                      "~/Scripts/jquery.signalR-{version}.js"));
    

    But you will need to add "/signalr/hubs" it in your view:

    @section Scripts {
        @Scripts.Render("~/bundles/signalr")
        @Scripts.Render("/signalr/hubs")
    }
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-13 08:58

    In my case, I lost Microsoft.Owin.Host.SystemWeb.dll & Microsoft.Owin.Security.dll in my project. After adding References to them, that error was solved. it's working now.

    0 讨论(0)
  • 2020-12-13 08:59

    Ok, I've found the issue, one thing I needed to do was:

    These two references were missing:

    Microsoft.AspNet.SignalR.Hosting.AspNet
    Microsoft.AspNet.SignalR.Hosting.Common
    

    Now, I included them getting nuget package: WebApiDoodle.SignalR which uses those two. My question is why those Dlls weren't added one I installed the Nuget Package: Microsoft.AspNet.SignalR -Pre?

    Bug?

    Then I had this in my global.asax.cs

    using SignalR;
    

    so

    RouteTable.Routes.MapHubs();
    

    was using that one, I needed to remove that using and use this one:

    using Microsoft.AspNet.SignalR;
    

    So the MapHubs use that one, and started to work.

    Hope this helps others. Guillermo.

    0 讨论(0)
  • 2020-12-13 09:05

    I had the same error message and resolved the issue by fixing a typo I had in the [HubName] attribute on the hub class - it was not exactly matching the property in the client-side javascript.

    C# hub class:

    [HubName("gameHub")]
    public class GameHub : Hub
    {
    

    client-side javascript:

    var foo = $.connection.gameHub;
    

    "gameHub" must be the same.

    hth

    0 讨论(0)
  • 2020-12-13 09:06

    I fixed that problem by changing my js code from: var myHub = $.connection.SentimentsHub; to var myHub = $.connection.sentimentsHub;

    So if you have some hub with class name TestHub you must use testHub(first letter is lowercase) name in js

    0 讨论(0)
  • 2020-12-13 09:06

    For ASP.Net MVC folks:

    Check your _Layout.cshtml: If you are calling the jquery bundle after the @RenderBody(), you will get this error.

    Resoultion: Just move the @Scripts.Render("~/bundles/jquery") to the head section or write all signalr scripts in the scripts "section"

    0 讨论(0)
提交回复
热议问题