Configuring Autofac with Owin Web API in AppBuilder.Map block with mixed MVC
I want to move the web API code to a .Map block ("/ api" inner =>) so that I can set the following configuration so as not to affect the MVC parts of my application.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(Startup.OAuthBearerOptions.AuthenticationType))
So, I tried to enable web api logic in .Map, but now I have broken Autofac and will no longer resolve dependencies. that's what i originally had.
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);
}
Here is what I tried to do, but I couldn’t resolve autofac dependencies with the web api anymore.
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);
});
}
Also, if I do everything that looks wrong, let me know.
My problem was that WebApiConfig was still logging in
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 -- 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);
});
}