【问题标题】:map hubs in referenced project参考项目中的地图中心
【发布时间】:2018-01-26 03:41:58
【问题描述】:

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

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

在线connection.Start().Wait(); 我得到一个例外。以下是该行抛出的异常序列:

  1. 指定的注册表项不存在 System.IO.IOException
  2. 'MessageHub' Hub 无法解析 InvalidOperationException
  3. 远程服务器返回错误:(500) Internal Server Error WebException

引用项目中消息中心类的签名是public class MessageHub : Hub

更新:为了测试理论,我将集线器类从引用的项目移到了我的测试项目中,并更新了命名空间。有效。所以我认为这里的理论是合理的......默认集线器分辨率在引用的项目或单独的命名空间中找不到集线器。

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

【问题讨论】:

  • 您能否列出有关异常的更多详细信息?查看 DefaultHubActivator 它使用 System.Activator 没有什么不寻常的。您确定它是当前的引用并且不指向较旧的构建 dll 或类似的东西吗?
  • 您可以覆盖默认的 IAssemblyLocator 并返回包含集线器的程序集。

标签: signalr


【解决方案1】:

我想我已经找到了答案。

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

    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();
    }
}

这让我尝试简单地将我的外部程序集添加到当前域,因为虽然它被引用了,但它并没有被加载。

所以在调用 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();
        }
    }

Core.Chat 就是我正在使用的 Hub 类。 然后加载引用程序集中定义的集线器。

可能有更直接的方法可以解决此问题,但我在文档中找不到任何内容。

希望这会有所帮助。

【讨论】:

  • 感谢您跟踪此问题。我自己也遇到了完全相同的问题,并按照您的描述将程序集添加到应用程序域中解决了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 1970-01-01
  • 2016-03-16
  • 2011-01-22
  • 2020-02-28
  • 2011-06-01
  • 1970-01-01
相关资源
最近更新 更多