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

时间:2015-01-15 04:51:44

标签: asp.net-mvc asp.net-web-api autofac owin

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


config.SuppressDefaultHostAuthentication();
config.Filters.Add(new 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);
}

这是我尝试做的事情,但是由于web 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);
        });
}

此外,如果我做任何看起来不对的事,请告诉我。

1 个答案:

答案 0 :(得分:1)

我的问题是WebApiConfig仍在

中注册


protected void Application_Start() 
{
  GlobalConfiguration.Configure(WebApiConfig.Register);
}

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

我使用的最终配置:


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 -- http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/
        //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);
            });
}