asp.net-mvc - SignalR 无法从外部程序集找到集线器

标签 asp.net-mvc signalr

我正在编写小型 Web 应用程序,并想使用信号器创建推送通知,但是当我从其他程序集加载集线器时,什么也没有发生。

这是我的程序集定位器,来自引用我的 asp.net mvc 项目的其他库

public class HubAssemblyLocator : IAssemblyLocator {

    public IList<Assembly> GetAssemblies() {


        IList<Assembly> allAsms = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList();

        var executingDirectory = AppDomain.CurrentDomain.BaseDirectory;

        var assemblyFiles = Directory.EnumerateFiles( executingDirectory, "Application" );

        foreach ( var assemblyFile in assemblyFiles.Where( _ => _.EndsWith( ".dll" ) ) ) {

            var asm = Assembly.LoadFile( assemblyFile );

            if ( asm.GetTypes().Any( _ => _.BaseType == typeof( Hub ) ) ) {
                allAsms.Add( asm );
            }
        }

        return allAsms;
    }
}

这是我在 Global.asax 中的 ApplicationStart 事件
public class MvcApplication : System.Web.HttpApplication {

    protected void Application_Start() {

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register( GlobalConfiguration.Configuration );
        FilterConfig.RegisterGlobalFilters( GlobalFilters.Filters );
        RouteConfig.RegisterRoutes( RouteTable.Routes );
        InitializeRepositories.Initialize();



        var servicesPre = GlobalHost.DependencyResolver.GetServices( typeof( IAssemblyLocator ) );

        RouteTable.Routes.MapHubs(new HubConfiguration() { EnableCrossDomain = true});

        GlobalHost.DependencyResolver.Register( typeof( IAssemblyLocator ), () => new HubAssemblyLocator() );

        var servicesPost = GlobalHost.DependencyResolver.GetServices(typeof (IAssemblyLocator));
    }
}

在这个问题中这个问题解决了,但在我的情况下是行不通的。

SignalR IAssemblyLocator not fired

在我的情况下,程序集定位器未执行。

更新:只需在区域和路线注册之前注册集线器。

最佳答案

RouteTable.Routes.MapHubs() 应该在 RegisterRoutes() 之前调用,但在 Application_Start 中它是在之后调用的。您还可以在调用 MapHubs() 之后注册 HubAssemblyLocator,但从逻辑上讲,它应该先注册。

关于asp.net-mvc - SignalR 无法从外部程序集找到集线器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292205/

相关文章:

c# - $.connections 无法读取未定义的集线器 (SignalR)

javascript - 是否可以在 Web Worker 中创建 signalR 流连接

asp.net - 有没有办法在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色?

asp.net-mvc - Onion架构同层依赖: Infrastructure and Web communicating

asp.net-mvc - @Html.PagedListPager 添加 CSS 类

asp.net-mvc - SignalR 从 Controller 获取用户 ID

asp.net - 使用 HttpContextScoped() 时 StructureMap 不会处理数据上下文

asp.net-mvc - 如何分配数据层级过滤器

javascript - 当服务器调用客户端时,如何在 javascript 中引发事件?

SignalR 从 javascript 客户端连接速度很慢