Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

signalr - map hubs in referenced project

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...