map hubs in referenced project

2020-02-05 06:20发布

问题:

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 project code that spins up the self-hosted application.

On the line connection.Start().Wait(); I get an exception. The following is the sequence of exceptions thrown at that line:

  1. The specified registry key does not exist System.IO.IOException
  2. 'MessageHub' Hub could not be resolved InvalidOperationException
  3. The remote server returned an error: (500) Internal Server Error WebException

The signature of the message hub class in the referenced project is public class MessageHub : Hub.

Update: To test the theory, I moved the hub class from the referenced project into my test project and updated the namespace. It worked. So I think the theory here is sound... default hub resolution does not find hubs in referenced project or in separate namespace.

How can I convince MapHubs to find the test hub in the referenced project?

回答1:

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.



标签: signalr