【问题标题】:Autofac setup with Owin Web API in a AppBuilder.Map block with mixed MVC在具有混合 MVC 的 AppBuilder.Map 块中使用 Owin Web API 设置 Autofac
【发布时间】:2015-01-16 07:30:08
【问题描述】:

我想将我的 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);
        });
}

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

【问题讨论】:

  • 我认为我的问题归结为仍在 Application_Start 中配置 GlobalConfiguration.Configure(WebApiConfig.Register);

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


【解决方案1】:

我的问题是 WebApiConfig 仍在注册


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

删除它并 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 -- @987654321@
        //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);
            });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    相关资源
    最近更新 更多