title: Autofac and Automapper
body: <p>I'm confused about the extent to which Autofac can be used with Automapper to map objects. I've read quite a bit of material online about integrating the two packages, but almost all seem to focus on how to create instances of IMapper from Automapper Profiles, using code something like this, which defines an Autofac Module (CSContainer.Instance is a static instance of Autofac's IContainer):</p>
  
  <pre><code>public class AutoMapperModule : Module
  {
      private static object ServiceConstructor( Type type )
      {
          return CSContainer.Instance.Resolve( type );
      }
  
      protected override void Load( ContainerBuilder builder )
      {
          base.Load( builder );
  
          var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  
          builder.RegisterAssemblyTypes( assemblies )
              .Where( t =&gt; typeof(Profile).IsAssignableFrom( t ) &amp;&amp; !t.IsAbstract &amp;&amp; t.IsPublic )
              .As&lt;Profile&gt;();
  
          builder.Register( c =&gt; new MapperConfiguration( cfg =&gt;
              {
                  cfg.ConstructServicesUsing( ServiceConstructor );
  
                  foreach( var profile in c.Resolve&lt;IEnumerable&lt;Profile&gt;&gt;() )
                  {
                      cfg.AddProfile( profile );
                  }
              } ) )
              .AsSelf()
              .AutoActivate()
              .SingleInstance();
  
          builder.Register( c =&gt; c.Resolve&lt;MapperConfiguration&gt;().CreateMapper( c.Resolve ) )
              .As&lt;IMapper&gt;()
              .SingleInstance();
      }
  }
  </code></pre>
  
  <p>(see <a href="http://www.protomatter.co.uk/blog/development/2017/02/modular-automapper-registrations-with-autofac/" rel="nofollow noreferrer">http://www.protomatter.co.uk/blog/development/2017/02/modular-automapper-registrations-with-autofac/</a> for an explanation of this approach)</p>
  
  <p>What I'd like to do is have Automapper call Autofac to create objects. Right now the only way I can see to do this is by doing something like this:</p>
  
  <p>CreateMap()
          .ConstructUsing( src => CSContainer.Instance.Resolve() );</p>
  
  <p>This works, but feels kludgy. It'd be neater if Automapper tried to discover how to resolve instances using Autofac automagically, behind the scenes as it were.</p>
  
  <p>I thought something like this might do the trick (this is a modified version of that first Autofac Module I cited above):</p>
  
  <pre><code>public class AutoMapperModule : Module
  {
      protected override void Load( ContainerBuilder builder )
      {
          base.Load( builder );
  
          var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  
          builder.RegisterAssemblyTypes( assemblies )
              .Where( t =&gt; typeof(Profile).IsAssignableFrom( t ) &amp;&amp; !t.IsAbstract &amp;&amp; t.IsPublic )
              .As&lt;Profile&gt;();
  
          builder.Register( c =&gt; new MapperConfiguration( cfg =&gt;
              {
                  cfg.ForAllMaps( ( map, opts ) =&gt;
                      opts.ConstructUsing( ( x, y ) =&gt; CSContainer.Instance.Resolve(map.DestinationType) ) );
  
                  foreach( var profile in c.Resolve&lt;IEnumerable&lt;Profile&gt;&gt;() )
                  {
                      cfg.AddProfile( profile );
                  }
              } ) )
              .AsSelf()
              .AutoActivate()
              .SingleInstance();
  
          builder.Register( c =&gt; c.Resolve&lt;MapperConfiguration&gt;().CreateMapper( c.Resolve ) )
              .As&lt;IMapper&gt;()
              .SingleInstance();
      }
  </code></pre>
  
  <p>but this resulted in Autofac throwing an exception, complaining about me trying to re-use a builder that had already been defined (apologies, I don't have the exact wording handy).</p>
  
  <p>Is it possible to integrate Automapper and Autofac so that Automapper resolves new instances via Autofac? If so, what's the best way to do it?</p>
  
  <p><strong>Additional Info</strong></p>
  
  <p>So I implemented the suggested answer as follows:</p>
  
  <pre><code>    protected override void Load( ContainerBuilder builder )
      {
          base.Load( builder );
  
          var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  
          builder.RegisterAssemblyTypes( assemblies )
              .Where( t =&gt; typeof(Profile).IsAssignableFrom( t ) &amp;&amp; !t.IsAbstract &amp;&amp; t.IsPublic )
              .As&lt;Profile&gt;();
  
          builder.Register( c =&gt; new MapperConfiguration( cfg =&gt;
              {
                  cfg.ConstructServicesUsing( ServiceConstructor );
  
                  foreach( var profile in c.Resolve&lt;IEnumerable&lt;Profile&gt;&gt;() )
                  {
                      cfg.AddProfile( profile );
                  }
              } ) )
              .AsSelf()
              .AutoActivate()
              .SingleInstance();
  
          builder.Register( c =&gt;
              {
                  // these are the changed lines
                  var scope = c.Resolve&lt;ILifetimeScope&gt;();
                  return new Mapper(c.Resolve&lt;MapperConfiguration&gt;(), scope.Resolve );
              } )
              .As&lt;IMapper&gt;()
              .SingleInstance();
      }
  </code></pre>
  
  <p>But this leads to an Automapper exception, complaining about the object I'm trying to create via a call to Map() must have zero arguments, or only optional arguments. Yet all the object's constructor arguments are also registered with Autofac, and it has no problem creating instances of the objects by itself elsewhere in my code. It's just when Automapper tries to create an instance that things go haywire.</p>