Signalr 参考项目中的地图中心

Signalr 参考项目中的地图中心,signalr,Signalr,在我的例子中,我的集线器位于一个从项目代码引用的项目中,该项目代码启动自托管应用程序 在线connection.Start().Wait()上我得到一个异常。以下是在该行引发的异常序列: 指定的注册表项不存在System.IO.IOException 无法解析“MessageHub”集线器无效操作异常 远程服务器返回错误:(500)内部服务器错误WebException 引用项目中消息中心类的签名是公共类MessageHub:hub 更新:为了测试理论,我将hub类从引用的项目移动到了测试项目中

在我的例子中,我的集线器位于一个从项目代码引用的项目中,该项目代码启动自托管应用程序

在线
connection.Start().Wait()上我得到一个异常。以下是在该行引发的异常序列:

  • 指定的注册表项不存在System.IO.IOException
  • 无法解析“MessageHub”集线器无效操作异常
  • 远程服务器返回错误:(500)内部服务器错误WebException
  • 引用项目中消息中心类的签名是
    公共类MessageHub:hub

    更新:为了测试理论,我将hub类从引用的项目移动到了测试项目中,并更新了名称空间。成功了。所以我认为这里的理论是正确的。。。默认集线器解析在引用的项目或单独的命名空间中找不到集线器


    如何说服MapHubs在引用的项目中找到测试中心?

    我想我已经找到了答案

    在对源代码进行了一些挖掘之后,Signal似乎使用以下方法指定一个IAssemblyLocator来定位集线器

        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();
        }
    }
    
    内部静态RouteBase MapHubs(此RouteCollection路由、字符串名称、字符串路径、HUB配置、操作生成)
    {
    var locator=newlazy(()=>newbuildmanageRassemblyLocator());
    configuration.Resolver.Register(typeof(iasemblylocator),()=>locator.Value);
    初始化保护数据(配置);
    返回路由.MapOwinPath(名称、路径、映射=>
    {
    建造(地图);
    MapHubs(String.Empty,配置);
    });
    }
    公共类BuildManagerAssemblyLocator:DefaultAssemblyLocator
    {
    公共重写IList GetAssemblys()
    {
    返回BuildManager.getReferencedAssemblys().Cast().ToList();
    }
    }
    公共类DefaultAssemblyLocator:IAssemblyLocator
    {
    公共虚拟IList getAssemblys()
    {
    返回AppDomain.CurrentDomain.GetAssemblys();
    }
    }
    
    这让我尝试简单地将我的外部程序集添加到当前域,因为虽然它被引用,但并没有被加载

    所以在调用WebApp.Start之前,我先调用下面的线路

        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();
            }
        }
    
    static void Main(字符串[]args)
    {
    字符串url=”http://localhost:8080";
    //添加这一行
    AppDomain.CurrentDomain.Load(typeof(Core.Chat.Assembly.FullName);
    使用(WebApp.Start(url))
    {
    WriteLine(“在{0}上运行的服务器”,url);
    Console.ReadLine();
    }
    }
    
    其中Core.Chat只是我正在使用的Hub类。 然后加载引用部件中定义的轮毂

    可能有一种更直接的方法来解决这个问题,但我在文档中找不到任何东西


    希望这能有所帮助。

    您能列出有关此异常的更多详细信息吗?看看DefaultHubActivator它使用的是System.Activator没有什么不寻常的。您确定引用是当前的并且没有指向旧的生成dll或类似的东西吗?您可以覆盖默认的IAssemblyLocator并返回包含集线器的程序集。感谢您跟踪此问题。我自己也遇到了同样的问题,并将程序集添加到应用程序域中,正如您所描述的那样解决了这个问题。