在具有混合 MVC 的 AppBuilder.Map 块中使用 Owin Web API 设置 Autofac

Autofac setup with Owin Web API in a AppBuilder.Map block with mixed MVC

我想将我的 Web API 代码移动到 .Map("/api" inner=>) 块中,这样我就可以设置以下配置以不影响 MVC 部分我的应用程序。


config.SuppressDefaultHostAuthentication();
config.Filters.Add(新的 HostAuthenticationFilter(Startup.OAuthBearerOptions.AuthenticationType))

所以我尝试将 Web api 逻辑放入 .Map 中,但现在我破坏了 Autofac,它将不再解析依赖关系。这是我原来的。


public void Configuration(IAppBuilder app)
{
    var config = new HttpConfiguration();
    var builder = new ContainerBuilder();
    BuildContainer(builder);
    this._container = builder.Build();
    // Middlewares
    app.UseAutofacMiddleware(this._container); //should be the first middleware added to IAppBuilder
    app.UseAutofacMvc();     
    // WebApi config
    config.DependencyResolver = new AutofacWebApiDependencyResolver(this._container);

    //MVC
    DependencyResolver.SetResolver(new AutofacDependencyResolver(this._container)); 
    this.ConfigureAuth(app, _container.Resolve());
    WebApiConfig.Register(config);
    app.UseAutofacWebApi(config); 
    app.UseWebApi(config);
}

这是我尝试做的但因网络失败而失败 api 不再解析 autofac 依赖项。


public void Configuration(IAppBuilder app)
{
    var builder = new ContainerBuilder();
    BuildContainer(builder);
    this._container = builder.Build();
    // Middlewares
    app.UseAutofacMiddleware(this._container); //should be the first middleware added to IAppBuilder
    app.UseAutofacMvc(); 
    //MVC
    DependencyResolver.SetResolver(new AutofacDependencyResolver(this._container)); 
    this.ConfigureAuth(app, _container.Resolve());

    app.Map(
        "/api",
        inner =>
            {
            var config = new HttpConfiguration();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(this._container);
            WebApiConfig.Register(config);
            //inner.UseAutofacMiddleware(this._container); //should be the first middleware added to IAppBuilder
            inner.UseAutofacWebApi(config); //Web.API enable lifetime scope created during the OWIN request to extend into WebAPI.
            inner.UseWebApi(config);
        });
}

此外,如果我做的事情看起来有误,请告诉我。

我的问题是 WebApiConfig 仍在

中注册


protected void Application_Start() 
{
  GlobalConfiguration.Configure(WebApiConfig.Register);
}
</pre>

删除这个和 autofac 连接地图下的所有重要内容。奇怪的是在没有地图的情况下在那里注册允许一切正常工作(也许还有其他我不知道的副作用)。

我使用的最终配置:


public void Configuration(IAppBuilder app)
{
        var builder = new ContainerBuilder();
        BuildContainer(builder);
        this._container = builder.Build();
        // OWIN Middlewares
        app.UseAutofacMiddleware(this._container); //should be the first middleware added to IAppBuilder
        app.UseAutofacMvc(); // Autofac MVC Integration -- <a href="http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/" rel="nofollow">http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/</a>
        //MVC
        DependencyResolver.SetResolver(new AutofacDependencyResolver(this._container)); //TODO: Still needed with OWIN?
        //app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        this.ConfigureAuth(app, _container.Resolve());
        //WEB API config - Make Web Api urls use the config, avoiding ASP.NET MVC from inheriting the HttpConfig.
        app.Map(
            "/api",
            inner =>
                {
                var config = new HttpConfiguration();
                config.DependencyResolver = new AutofacWebApiDependencyResolver(this._container);
                WebApiConfig.Register(config);
                inner.UseAutofacWebApi(config); //Web.API enable lifetime scope created during the OWIN request to extend into WebAPI.
                inner.UseWebApi(config);
            });
}
</pre>