问题
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)
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
});
}
}
}
回答1:
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.
回答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.
来源:https://stackoverflow.com/questions/19626244/signalr-2-0-in-vs2012-registration-and-dependency-injection