C# 信号机、webapi和autofac

C# 信号机、webapi和autofac,c#,asp.net-web-api2,owin,autofac,C#,Asp.net Web Api2,Owin,Autofac,我有一个Signalr2项目也使用WebApi,我正在尝试将AutoFac集成到这个项目中 最初,我的Startup类是这样的: public class Startup { public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.MapSignalR();

我有一个Signalr2项目也使用WebApi,我正在尝试将AutoFac集成到这个项目中

最初,我的Startup类是这样的:

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);
    }
}
    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);            
    }
不使用autofac,一切正常。现在,我正在尝试添加AutoFac,因此我将我的Startup类更改为如下所示:

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);
    }
}
    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,这在以前是有效的。我错过了什么?在的《autofac快速启动指南》中,有一个对app.UseAutofacWebApi(config)的调用,但是,该方法不存在,因此,不确定这是否是问题所在

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

虽然我也改变了一些其他的事情。我是这样做的。 var builder=new ContainerBuilder()

//注册控制器,以便注入依赖项
注册控制器(类型化(MVCAPApplication).Assembly);
RegisterAppController(Assembly.getExecutionGassembly());
//注册信号器集线器。
builder.RegisterType().ExternallyOwned();
builder.RegisterType().ExternallyOwned();
//构建容器
var container=builder.Build();
//使用Autofac依赖项解析程序的实例配置SignalR。
GlobalHost.DependencyResolver=新的Autofac.Integration.Signal.AutofacDependencyResolver(容器);
SetResolver(新的Autofac.Integration.Mvc.AutofacDependencyResolver(容器));
GlobalConfiguration.Configuration.DependencyResolver=新的AutoFacWebApidencyResolver((IContainer)容器);
//向OWIN注册
app.useautofac中间件(容器);
app.UseAutofacMvc();
app.mapsigner();
ConfigureAuth(app);
我犯的另一个错误是认为集线器本身可以注入控制器。您不能这样做,因此如果您需要这样做,您必须通过中心上下文来完成,我需要找到一种方法来注入中心上下文。这是我的待办事项

var updateHubContext = GlobalHost.ConnectionManager.GetHubContext<UpdateHub>();
updateHubContext.Clients.All.update();
var updateHubContext=GlobalHost.ConnectionManager.GetHubContext();
updateHubContext.Clients.All.update();
如果需要生存期作用域,则必须使用接口ILifetimeScope将依赖项解析到中心。您需要注入生存期范围,然后从中解析它们

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>>();
}
专用只读ILifetimeScope\u hubLifetimeScope;
私有只读IEntityService_updateService;
公共更新HUB(ILifetimeScope生存时间范围)
{
//AutofacWebRequest很重要。没有它,它将无法工作
_hubLifetimeScope=lifetimeScope.BeginLifetimeScope(“AutofacWebRequest”);
_updateService=_hubLifetimeScope.Resolve();
}

希望这能有所帮助。javascript与网站上的教程相同。那里没有任何变化。

因此,根据WebAPI OWIN集成文档,您添加了Autofac.WebApi2.OWIN包,而UseAutofacWebApi扩展不在那里?你需要这个。你能再检查一下吗?