Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map hubs in referenced project

Tags:

signalr

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?

like image 549
cocogorilla Avatar asked May 27 '13 19:05

cocogorilla


People also ask

What is Hub in c#?

The SignalR Hubs API enables you to make remote procedure calls (RPCs) from a server to connected clients and from clients to the server. In server code, you define methods that can be called by clients, and you call methods that run on the client.

What is Hub in SignalR?

Hubs are SignalR's high level API that allow both realtime client-to-server and server-to-client RPC over HTTP. The hub supports 1-to-many RPC e.g.: all clients, groups of clients, just the caller etc. Transport is over one of the following (best to worst): WebSocket, server sent events, forever frame, long polling.

Can SignalR be used in Web API?

SignalR can be used together with Web API just fine, so some functionality in application which is not realtime and doesn't require persistent connection could be served by Web API.

Is SignalR asynchronous?

SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.


1 Answers

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.

like image 198
tomasat Avatar answered Sep 27 '22 16:09

tomasat