map hubs in referenced project

前端 未结 1 756
执笔经年
执笔经年 2020-12-10 03:24

http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-signalr-20-self-host

In my case, my hubs are in a project referenced from the

相关标签:
1条回答
  • 2020-12-10 03:46

    I think that I have found an answer to this.

    After doing some digging around the source code it seems that SignalR uses the following method to designate an IAssemblyLocator to locate Hubs.

        internal static RouteBase MapHubs(this RouteCollection routes, string name, string path, HubConfiguration configuration, Action<IAppBuilder> build)
        {
            var locator = new Lazy<IAssemblyLocator>(() => new BuildManagerAssemblyLocator());
            configuration.Resolver.Register(typeof(IAssemblyLocator), () => locator.Value);
    
            InitializeProtectedData(configuration);
    
            return routes.MapOwinPath(name, path, map =>
            {
                build(map);
                map.MapHubs(String.Empty, configuration);
            });
        }
    
    public class BuildManagerAssemblyLocator : DefaultAssemblyLocator
    {
        public override IList<Assembly> GetAssemblies()
        {
            return BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList();
        }
    }
    
    public class DefaultAssemblyLocator : IAssemblyLocator
    {
        public virtual IList<Assembly> GetAssemblies()
        {
            return AppDomain.CurrentDomain.GetAssemblies();
        }
    }
    

    This got me to try to simply add my external assembly to current domain because although it was referenced it was not being loaded.

    So before calling WebApp.Start I call the following line.

        static void Main(string[] args)
        {
            string url = "http://localhost:8080";
    
            // Add this line
            AppDomain.CurrentDomain.Load(typeof(Core.Chat).Assembly.FullName);
    
            using (WebApp.Start<Startup>(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    

    Where Core.Chat is simply the Hub class I'm using. And then the hubs defined in referenced assembly are loaded.

    There might be a more straight forward way to go about this but I could not find anything in the documentation.

    Hope this helps.

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