Is it possible to configure Autofac to work with ASP.NET MVC and ASP.NET Web Api

asp.net Mvcasp.net Web-ApiAutofac

asp.net Mvc Problem Overview


Is it possible to configure Autofac to work with ASP .NET MVC and ASP .NET Web Api. I'm aware that the dependency resolvers are different. But when using the documented approaches I can only set one global resolver.

// Set the dependency resolver implementation.
GlobalConfiguration.Configuration.DependencyResolver = resolver;

Is this approach bad idea? Should I separate my solution into two projects and handle the dependency injection for each individually?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

It is certainly possible to configure Autofac to work with both MVC and Web API. This is expected to be a very common scenario. There are two separate dependency resolver implementations because MVC and Web API can be used independently of one another. The same applies for the Autofac integrations.

When using both MVC and Web API in the same application each will require its own dependency resolver, though they can be provided with the same instance of the container.

var builder = new ContainerBuilder();

// Add your registrations

var container = builder.Build();

// Set the dependency resolver for Web API.
var webApiResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;

// Set the dependency resolver for MVC.
var mvcResolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(mvcResolver);

It is also possible to share registrations between the two because the InstancePerApiRequest and InstancePerHttpRequest lifetime scopes now share the same tag.

Note that the mechanism for setting the dependency resolver for Web API and MVC is different. Web API uses GlobalConfiguration.Configuration.DependencyResolver and MVC uses DependencyResolver.SetResolver.

Solution 2 - asp.net Mvc

I thought I'd add a little help those struggling with this in mvc5 and web api 2.

First add nuget packages

  • Autofac
  • Autofac asp.net mvc 5 integration
  • Autofac asp.net web api 2.x integration

in global add in application_start (or as app_start class) add call to the below class

AutofacConfig.RegisterAutoFac();

now add this class under App_start

using System.Reflection;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;

namespace Example1.Web
{
	public class AutofacConfig
	{
		public static IContainer RegisterAutoFac()
		{
			var builder = new ContainerBuilder();

			AddMvcRegistrations(builder);
			AddRegisterations(builder);

			var container = builder.Build();

			DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
			GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

			return container;
		}

		private static void AddMvcRegistrations(ContainerBuilder builder)
		{
			//mvc
			builder.RegisterControllers(Assembly.GetExecutingAssembly());
			builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
			builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
			builder.RegisterModelBinderProvider();

			//web api
			builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
			builder.RegisterModule<AutofacWebTypesModule>();
		}

		private static void AddRegisterations(ContainerBuilder builder)
		{
			//builder.RegisterModule(new MyCustomerWebAutoFacModule());
		}
	}
}

From now for each new assembly you add to the project add a new module and then register the module in the AddRegisterations function (example given)

Note:

I returned the container, this isn't necessary.

This scans the current assembly for modules so don't add local modules in AddRegisterations otherwise you will register everything twice.

Solution 3 - asp.net Mvc

Definitely separate them. Autofac has both ASP.NET MVC and ASP.NET Web API integrations. This is not always the case but if you need the same services in both application, most probably there is something wrong with the application architecture.

Here is how you might do this with ASP.NET Web API:

internal class AutofacWebAPI {

    public static void Initialize(HttpConfiguration config) {

        config.DependencyResolver = new AutofacWebApiDependencyResolver(
            RegisterServices(new ContainerBuilder())
        );
    }

    private static IContainer RegisterServices(ContainerBuilder builder) {

        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();

        //deal with your dependencies here
        builder.RegisterType<CarsService>().As<ICarsService>();
        builder.RegisterType<CarsCountService>().As<ICarsCountService>();

        return builder.Build();
    }
}

Then, register this inside the Global.asax.cs as below:

AutofacWebAPI.Initialize(GlobalConfiguration.Configuration);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRa&#250;l RoaView Question on Stackoverflow
Solution 1 - asp.net MvcAlex Meyer-GleavesView Answer on Stackoverflow
Solution 2 - asp.net MvcChoco SmithView Answer on Stackoverflow
Solution 3 - asp.net MvctugberkView Answer on Stackoverflow