Rectangle 27 0

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


DependencyResolver.SetResolver
GlobalConfiguration
GlobalConfiguration.Configuration.DependencyResolver
HttpConfiguration
config.DependencyResolver = new AutofacWebApiDependencyResolver(...)
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);

Great answer. The only thing is about "because MVC and Web API can be used independently of one another.". I reckon autofac has two plugins for mvc and webapi separately because the two have different runtime.

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

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.

Note
Rectangle 27 0

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


AutofacConfig.RegisterAutoFac();
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)

I returned the container, this isn't necessary.

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

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

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

thankyou. Finally someone that just lays it out here i na simple an easy to understand way without imposing opinions etc.

Note
Rectangle 27 0

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


AutofacConfig.RegisterAutoFac();
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)

I returned the container, this isn't necessary.

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

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

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

thankyou. Finally someone that just lays it out here i na simple an easy to understand way without imposing opinions etc.

Note
Rectangle 27 0

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


DependencyResolver.SetResolver
GlobalConfiguration
GlobalConfiguration.Configuration.DependencyResolver
HttpConfiguration
config.DependencyResolver = new AutofacWebApiDependencyResolver(...)
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);

Great answer. The only thing is about "because MVC and Web API can be used independently of one another.". I reckon autofac has two plugins for mvc and webapi separately because the two have different runtime.

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

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.

Note
Rectangle 27 0

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


AutofacWebAPI.Initialize(GlobalConfiguration.Configuration);
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();
    }
}

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:

That's true. It's better to separate the front end and the services configuration in the same project.

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

Note
Rectangle 27 0

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


AutofacWebAPI.Initialize(GlobalConfiguration.Configuration);
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();
    }
}

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:

That's true. It's better to separate the front end and the services configuration in the same project.

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

Note
Rectangle 27 0

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


AutofacConfig.RegisterAutoFac();
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)

I returned the container, this isn't necessary.

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

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

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

thankyou. Finally someone that just lays it out here i na simple an easy to understand way without imposing opinions etc.

Note
Rectangle 27 0

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


AutofacWebAPI.Initialize(GlobalConfiguration.Configuration);
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();
    }
}

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:

That's true. It's better to separate the front end and the services configuration in the same project.

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

Note
Rectangle 27 0

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


DependencyResolver.SetResolver
GlobalConfiguration.Configuration.DependencyResolver
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);

Great answer. The only thing is about "because MVC and Web API can be used independently of one another.". I reckon autofac has two plugins for mvc and webapi separately because the two have different runtime.

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

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.

Note