关于C#:Signalr,WebAPI和Autofac

signalr, webapi and autofac

我有一个SignalR 2项目,该项目也使用WebApi,并且我正在尝试将AutoFac集成到其中。

最初,我的Startup类如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Startup
{
    public void Configuration(IAppBuilder app)
    {          
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        app.MapSignalR();

        var httpConfig = new HttpConfiguration();

        httpConfig.MapHttpAttributeRoutes();

        app.UseWebApi(httpConfig);
    }
}

它不使用autofac,并且一切正常。 现在,我尝试添加AutoFac,因此我将Startup类更改为如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();

        var httpConfig = new HttpConfiguration();
        var hubConfig = new HubConfiguration();

        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterHubs(Assembly.GetExecutingAssembly());

        var container = builder.Build();
        httpConfig.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        hubConfig.Resolver = new AutofacDependencyResolver(container);

        app.UseAutofacMiddleware(container);
        app.MapSignalR("/signalr", hubConfig);
        app.UseWebApi(httpConfig);            
    }

现在发生的事情是我无法调用控制器,因为每次调用时都会得到404,这在以前是可行的。 我想念什么? 在Web API的autofac快速入门指南中,有一个对app.UseAutofacWebApi(config)的调用,但是,该方法不存在,因此,不知道这是问题还是什么。


我遇到过同样的问题。 对于我来说,我必须单独添加集线器,并使用Nuget软件包Autofac.SignalR版本3.0.2和SignalR.Extras.Autofac v1.2.0。 有几个Autofac SignalR软件包,请确保仅这两个软件包。

尽管我也更改了其他一些内容。 这是我的方法。
var builder = new ContainerBuilder();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// REGISTER CONTROLLERS SO DEPENDENCIES ARE CONSTRUCTOR INJECTED
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

// Register the SignalR hubs.
builder.RegisterType<UpdateHub>().ExternallyOwned();
builder.RegisterType<NotificationHub>().ExternallyOwned();

// BUILD THE CONTAINER
var container = builder.Build();

// Configure SignalR with an instance of the Autofac dependency resolver.
GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container);

// REGISTER WITH OWIN
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
app.MapSignalR();

ConfigureAuth(app);

我犯的另一个错误是认为集线器本身可以注入到控制器中。 您无法做到这一点,因此,如果需要,您必须在中心环境中进行操作,我需要找到一种方法来注入中心环境。 那是我的任务。

1
2
var updateHubContext = GlobalHost.ConnectionManager.GetHubContext<UpdateHub>();
updateHubContext.Clients.All.update();

而且,如果需要生存期范围,则必须使用ILifetimeScope接口将依赖项解析到集线器中。 您需要注入Lifetime Scope,然后从中解决它们。

1
2
3
4
5
6
7
8
9
private readonly ILifetimeScope _hubLifetimeScope;
private readonly IEntityService<Update> _updateService;

public UpdateHub(ILifetimeScope lifetimeScope)
{
   //the AutofacWebRequest is important. It will not work without it
    _hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");
    _updateService = _hubLifetimeScope.Resolve<IEntityService<Update>>();
}

希望这会有所帮助。 javascript与站点上的教程相同。 那里什么都没有改变。