【问题标题】:SignalR: Error loading hubs. 404 not foundSignalR:加载集线器时出错。 404 未找到
【发布时间】:2018-06-28 13:11:42
【问题描述】:

我正在使用 Angular 5 应用程序,我正在尝试实现 SignalR。而且我收到了 2 个错误,我认为它们是同一件事。首先是:

加载资源失败:服务器响应状态为 404(未找到)

第二个是:

ERROR 错误:SignalR:加载集线器时出错。确保您的集线器参考正确,例如.

也就是说,我一直在对此进行研究,并看到了一些主要引用旧版本 siganalR 的选项。

我已关注Tutorial 并添加了一个集线器

[HubName("EventContractHub")]
public class EventContractHub : Hub, IClientHub
{
    public void SendId(int id)
    {
        Clients.All.Send(id);
    }
}

Startup.cs 类看起来像...

[assembly:OwinStartUp(typeof(blah.Startup))]
namespace blah
{
   public class Startup
   {
      public void Configuration(IAppBuilder app)
      {
          // Other configuration here
          app.MapSignalR();
      }
   }
}

我的_Layout.cshtml 有一个看起来像这样的引用:

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>

有谁知道为什么现在会发生这种情况?

404 not found url 是:http://localhost/IntelliM/signalr/hubs

我应该提一下,我也有一个使用 Owin 在同一台服务器上运行的 api。我不知道这是否会改变什么。如果是这样,我也可以发布配置。

请理解我必须删除这里的一些代码,我留下了相关代码:

   public void Configuration(IAppBuilder app)
    {

        if (/*Logic for License*/)
        {
            app.Map("/identity", ssocf =>
            {

            });
        }



        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies"
        });



        app.UseOpenIdConnectAuthenticationPatched(new OpenIdConnectAuthenticationOptions
        {


            SignInAsAuthenticationType = "Cookies",

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthorizationCodeReceived = async n =>
                {

                    /*
                     * Add Clams here
                     */
                },

                SecurityTokenValidated = async n =>
                {

                        /*Create Token Here*/
                },

                RedirectToIdentityProvider = n =>
                {

                }
            }
        });


        //Setting WebAPI Auth
        var config = new HttpConfiguration();
        config.Formatters.Remove(config.Formatters.XmlFormatter);
        //config.SuppressDefaultHostAuthentication();

        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = authority
        });

        app.UseWebApi(config);
        app.MapSignalR();
    }

}

【问题讨论】:

  • 不,不是,我试过~/signalr/hubssignalr/hubs/intellim/signalr/hubs
  • 如果您阅读了同一个链接教程中的 cmets,那么其他人也会遇到同样的问题。检查以确保您调用的是正确的 URL 路径。
  • 还要确保您引用了正确版本的 jquery
  • 你提到有另一个 API。 SignalR 何时注册与该服务相关的?它们添加到管道中的顺序也很重要。
  • API注册前后我都试过了。我也会添加 Api 注册。

标签: c# signalr


【解决方案1】:

查看链接教程中的注释

重要

将 SignalR 和其他脚本库添加到 Visual Studio 项目时,包管理器可能会安装比本主题中显示的版本更新的 SignalR 脚本文件版本。确保代码中的脚本引用与项目中安装的脚本库的版本相匹配。

因此,请首先确保您引用了正确的版本。

考虑将 SignalR 相关引用添加到包中

public class BundleConfig {
    public static void RegisterBundles(BundleCollection bundles) {
        //... other bundles

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

        bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
            "~/Scripts/jquery.signalr-*", //the * wildcard will get script regardless of version
            "~/signalr/hubs"));

        //Enable minification
        BundleTable.EnableOptimizations = true;
    }
}

//...in start up
BundleConfig.RegisterBundles(BundleTable.Bundles);

在视图中

<!-- javascript: Placed at the end of the document so the pages load faster -->
@Scripts.Render("~/bundles/jquery", "~/bundles/signalr")

从路由的角度来看,您还想确保在添加到混合项目时没有其他冲突路由,因为添加到中间件管道的顺序会影响到中心的路由。

例如

public void Configuration(IAppBuilder app) {
    //Auth
    this.ConfigureAuth(app);
    
    //SignalR 
    app.MapSignalR();

    //...other configuration.
}

【讨论】:

  • 谢谢,更新 jquery 到 2.2.2 后我能找到 hubs 文件
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多