4

I have a scenario as such.

A base class which modules are derived from.
Each module has it's own signalr hubs.
I want to host all modules in a single host, separated by module name.
Some modules will share hub names.

namespace domain.com.base
{
    public class BaseClass
    {
        public string ApplicationName;
    }
}

namespace domain.com.billing
{
    public class Billing : BaseClass
    {
        ApplicationName = "Billing";
    }
    public class NotificationHub : Hub
    {
        public void Credit(decimal amount)
        {
            Clients.All.Notify(amount);
        }
    }
}

namespace domain.com.reporting
{
    public class Reporting : BaseClass
    {
        ApplicationName = "Reporting";
    }
    public class ReportingHub : Hub
    {
        public Report GetReport(int Id)
        {
             return ReportModule.RetrieveReport(Id);
        }
    }
}

In OWIN.Startup.Configuration(IAppBuilder) is it possible to do something like such

namespace domain.com.external_access
{
    public void Configuration(IAppBuilder app)
    {
        var asmList = GlobalResolver.Applications();
        foreach(var asm in asmList)
        {
            app.MapSignalR(asm.ApplicationName,false);
        }
    }
}

Effectively giving you something like so...

http://domain.com.external_access/Reporting/hubs http://domain.com.external_access/Billing/hubs

2
  • Have you tried: app.MapSignalR("/Reporting/signalr"); app.MapSignalR("/Billing/signalr");... ?
    – Pawel
    Commented Mar 2, 2015 at 21:15
  • 2
    That would be great, however, would it not resolve the same hubs at both endpoints? Commented Mar 3, 2015 at 6:50

1 Answer 1

5

Actually this is doable, even if it requires some heavy working-around SignalR's tight coupling around GlobalHost.


The answer below is based on what I remember, I don't have access to my code right now. I'll update the answer asap if errors are in there.

Edit: I had it right yesterday evening. Just do as written below


You'll need to implement your own IHubDescriptorProvider and IHubActivator in order to have control over which Hub are found for each of your "endpoints". Furthermore you need to provide each endpoint with their own instance of HubConfiguration (which inherits from ConnectionConfiguration) that doesn't use the global host dependency resolver. Here's what this could look like:

class CustomSignalRConnectionConfiguration : HubConfiguration
{
    public CustomSignalRConnectionConfiguration() 
    {
        this.Resolver = new DefaultDependencyResolver();

        // configure your DI here...
        var diContainer = new YourFavouriteDi();

        // replace IHubActivator
        this.Resolver.Register(
            typeof(IHubActivator), 
            () => diContainer.Resolve<IHubActivator>());

        // replace  IHubDescriptorProvider
        this.Resolver.Register(
            typeof(IHubDescriptorProvider), 
            () => diContainer.Resolve<IHubDescriptorProvider>());

    }
}

For your single endpoints you could then do something like:

app.Map("/endpointName", mappedApp => 
{   
    var config = new CustomSignalRConnectionConfiguration();
    mappedApp.MapSignalR(config);
});

Good luck!

1
  • Thanks! I will definitely try implement this. And let you know! Commented Mar 25, 2015 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.