SignalR: Error loading hubs

前提是你 提交于 2019-12-05 18:13:29

问题


Signalr doesn't load my hubs:

SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/js'></script>. 

I am calling app.MapSignalR(); in startup configuration.

I added to my cshtml:

<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/jquery.signalR-2.0.0.js"></script>
<script src="~/signalr/hubs" type="text/javascript"></script>

<script>

    $(document).ready(function () {
        window.hubReady = $.connection.hub.start();
    });

</script>

回答1:


Make sure your startup class has this attribute:

[assembly: OwinStartup(typeof(MyStartupClass))]

You can define your Owin startup class in your web.config as well:

<appSettings>  
    <add key="owin:appStartup" value="MyNamespace.MyStartupClass" />
</appSettings>



回答2:


Visit your site, ex http://localhost/signalr/hubs, and see if you can get a better error description there. My problem was that I had a generic method in my hub.

public void Update<T>(T objectToUpdate) where T : class



回答3:


Also make sure to add in your Startup class:

app.MapSignalR();

Solved my issue




回答4:


Server has to know where your startup class is

One option is like Rob wrotes:

[assembly: OwinStartup(typeof(MyStartupClass))]

But there are other possibilies up to your requiremens. From Microsoft Docs (docs.microsoft.com/en-us/aspnet/core/fundamentals/startup):

Alternatively, you can define a fixed Startup class that will be used regardless of the environment by calling UseStartup. This is the recommended approach.

Example:

public class Program
    {
        public static void Main(string[] args)
        {     
          BuildWebHost(args).Run();   
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }


来源:https://stackoverflow.com/questions/20144587/signalr-error-loading-hubs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!