绘制参照项目中的中心地图 [英] map hubs in referenced project

查看:15
本文介绍了绘制参照项目中的中心地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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’集线器InvalidOperationException
  3. 远程服务器返回错误:(500)内部服务器错误WebException

引用项目中消息集线器类的签名为public class MessageHub : Hub

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

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

推荐答案

我想我已经找到了这个问题的答案。

在对源代码进行了一些挖掘之后,SignalR似乎使用以下方法来指定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();
    }
}

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

因此在调用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类。 然后加载引用程序集中定义的集线器。

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

希望这对您有帮助。

这篇关于绘制参照项目中的中心地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆