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
            });
        }
    }
}
4

2 回答 2

9

您仍然可以像在 1.1.3 中一样设置依赖关系解析器。然而,更好的方法(做同样的事情,只是更清洁)是:

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

注意: app.MapSignalR()RouteTable.Routes.MapHubs()SignalR 2.0.0+ 的新功能,这意味着您不应该再做RouteTable.Routes.MapHubs().

现在对于您关于在 VS2012 中没有 Owin Startup 类的问题,没关系!只需创建一个新的空白类并将代码复制并粘贴到您的类中。无需其他设置。

于 2013-10-28T04:03:14.397 回答
2

我在下面使用了这种方法,而没有更改 HubConfiguration。

在 SignalR 2.0 中使用现有的 IoC 容器

我为 SignalR 和我的 Web 应用程序共享了容器,通过从 CustomHubActivator 解析 Hub,我可以在我的 Hub 中注入任何参数作为参数。

于 2014-05-26T04:45:30.913 回答