SignalR无法从外部装配中找到集线器

我编写小型Web应用程序,并希望使用signalr来创建推送通知,但是当我从其他程序集加载集线器时,什么都不会发生 .

这是我的程序集定位器,来自引用我的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

在我的情况下,程序集定位器没有执行 .

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

回答(1)

3 years ago

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