1

I was able to work with SignalR 1.13 with my own DI like this:

//Funq container
GlobalHost.DependencyResolver = new FunqDependencyResolver(container); 
RouteTable.Routes.MapHubs();

Now with the new version 2.0 I am stuck.

using Microsoft.Owin;
using Owin;
//SignalR 2.0 no longer uses RouteTable.Routes.MapHubs();
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat { 
    public class Startup {
        public void Configuration(IAppBuilder app) { app.MapSignalR(); }
    }
}

(New SignalR 2.0 setup in VS2013 screenshot)

enter image description here

Firstly, it's a screen from VS2013 from here. my VS2012 Pro doesn't have the Create New ...-> OWIN Startup class.I have hand written one. But now how do I call up the new startup class to replace the old MapHub() function?

Secondly, I was using the DI that runs the rest of the web project. How do I register signalR to my DI now?

EDIT --------------------------------------------

A bit more to the question. I create my DI container in Global.asax.cs->Application_Start(), but SignalR Startup.cs->Configuration() is automatically created and called. How do I pass my DI container to SignalR Startup?

Global.asax.cs (this runs automatically when app starts)

protected void Application_Start(object sender, EventArgs e)
{
    var appHost = new AppHost(); //DI init
    appHost.Init();
    var container = appHost.Container; //DI container here
    var resolver = new FunqDependencyResolver(container);
}

SignalRStarter.cs (this also runs automatically when app starts)

[assembly: OwinStartup(typeof(WebApp.SignalRStarter))]
namespace WebApp {
    public class SignalRStarter {
        public FunqDependencyResolver FunqDependencyResolver { get; set; }
        public bool EnableDetailedErrors { get; set; }

        public void Configuration(IAppBuilder app) {
            app.MapSignalR(new HubConfiguration() {
                EnableDetailedErrors = EnableDetailedErrors,
                Resolver = FunqDependencyResolver
            });
        }
    }
}
Tom
  • 15,781
  • 14
  • 69
  • 111
  • See http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/upgrading-signalr-1x-projects-to-20 – davidfowl Oct 28 '13 at 07:16

2 Answers2

9

You can still set the dependency resolver the same way you did in 1.1.3. However a better approach (does the same thing, just cleaner) would be to do:

app.MapSignalR(new HubConfiguration
{
    Resolver = new FunqDependencyResolver(container)
});

Note: app.MapSignalR() is the new RouteTable.Routes.MapHubs() for SignalR 2.0.0+, meaning you should no longer be doing RouteTable.Routes.MapHubs().

Now for your question about not having the Owin Startup class in VS2012, that's Ok! Just create a new blank class and copy n paste the code into your class. No other setup required.

N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • @N.Taylor Mullen, thank you. A bit more to the question there. I create my DI container in Global.asax.cs->Application_Start(), but SignalR Startup.cs->Configuration() is automatically created and called. How do I pass my DI container to SignalR Startup? (see edit above for source code) – Tom Oct 29 '13 at 23:20
  • 1
    Are you referring to the GlobalHost? – N. Taylor Mullen Oct 30 '13 at 03:23
  • @N. Taylor Mullen, oh yes, I can get my resolver in GlobalHost after I create the resolver and pass the container to it the old way :D thanks. – Tom Oct 30 '13 at 05:41
  • 1
    oh nonono the GlobalHost is a default dependency resolver, once you map signalr with your own resolver the GlobalHost resolver will be invalid. You can however set the GlobalHost dependency resolver to be the one that you set. – N. Taylor Mullen Oct 30 '13 at 06:26
  • 1
    @N. Taylor Mullen, this is what you mean, right? I did this in Application_Start() {//Funq container GlobalHost.DependencyResolver = new FunqDependencyResolver(container); ... So it would use my DI in the code you show me. – Tom Oct 30 '13 at 23:06
  • As long as that's before your MapSignalR call then yes all will be good. – N. Taylor Mullen Oct 31 '13 at 04:43
2

I used this approach below without changing HubConfiguration.

Using an existing IoC Container in SignalR 2.0

I shared the container for both SignalR and my web app, with resolving Hub from a CustomHubActivator, I can inject anything as parameters in my Hub.

Community
  • 1
  • 1
Adamy
  • 2,789
  • 3
  • 27
  • 25