question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AutoMapper 4.2 Injection with Autofac

See original GitHub issue

Could anyone help make the following example work with Autofac?

public class AutoMapperRegistry : Registry
{
    public AutoMapperRegistry()
    {
        var profiles =
            from t in typeof (AutoMapperRegistry).Assembly.GetTypes()
            where typeof (Profile).IsAssignableFrom(t)
            select (Profile)Activator.CreateInstance(t);

        var config = new MapperConfiguration(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });

        For<MapperConfiguration>().Use(config);
        For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
    }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:33 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
jbogardcommented, Feb 15, 2016

No idea, I’ve never used AutoFac.

5reactions
ehimahcommented, Feb 28, 2016

This worked for me

public class AutoMapperModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
          //register all profile classes in the calling assembly
           builder.RegisterAssemblyTypes(typeof(AutoMapperModule).Assembly).As<Profile>();

            builder.Register(context => new MapperConfiguration(cfg =>
            {
                foreach (var profile in context.Resolve<IEnumerable<Profile>>())
                {
                    cfg.AddProfile(profile);
                }
            })).AsSelf().SingleInstance();

            builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve))
                .As<IMapper>()
                .InstancePerLifetimeScope();
        }

// ~/App_Start/AutofacConfig.cs
 public class AutofacConfig
    {
        public static void Register()
        {
            var builder = new ContainerBuilder();

            // .....
            builder.RegisterModule(new AutoMapperModule());

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

// Global.asax
public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //  ......
            AutofacConfig.Register();
        }
    }

//In my controller

public class ProductLicensesController : Controller
    {

        private readonly IMapper mapper;

        public ProductLicensesController( IMapper mapper)
        {
            //. . .
            this.mapper = mapper;
        }

// then in some action method

var productLicense = mapper.Map<ProductLicense>(viewModel);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot resolve AutoMapper.IMapper using ...
While not exactly the answer, I had two regions in Startup, one for autofac and one for automapper. The builder.Build() was hidden and...
Read more >
AutoMapper 4.2.1
Install AutoMapper via Nuget Package Manager. ... Also install Autofac , to demonstrate passing of mapper object through Dependency Injection.
Read more >
Dependency Injection
There is a NuGet package to be used with the default injection mechanism described here and used in this project. You define the...
Read more >
Autofac support for AutoMapper
Resolving object graphs resulting from using dependency injection is a common task that should be automated. My choice for this is Autofac ......
Read more >
How to Use AutoMapper v10 in ASP.NET Web API 2 - YouTube
Automapper version 10 is the latest version, ... projects via dependency injection using the Unity dependency injection container in C#.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found