title: Having Automapper use services constructed by a Autofac with WebApi
body: <p>I'm using WebAPI + Autofac + Automapper, with a repository for data access. I need to map a model to my domain entities, specifically, I need to convert an identity value to the actual entity. No big deal, right? I've done this in MVC with no problem. I will simplify what I am doing to expose the essentials.</p>
  
  <pre><code>public class EntityConverter&lt;T&gt; : ITypeConverter&lt;int, T&gt;
          where T : Entity
  {
     public EntityConverter(IRepository&lt;T&gt; repository)
     {
         _repository = repository;
     }
  
     private readonly IRepository&lt;T&gt; _repository;
  
     public T Convert(ResolutionContext context)
     {
         _repository.Get((int) context.SourceValue);
     }
  }
  </code></pre>
  
  <p>Repositories are registered with Autofac, and are managed as <code>InstancePerApiRequest</code> because of session/transaction management. So, I need to register my converter in that same scope:</p>
  
  <pre><code> builder.RegisterGeneric(typeof(EntityConverter&lt;&gt;))
          .AsSelf()
          .InstancePerApiRequest();
  </code></pre>
  
  <p>The Automapper config looks something like:</p>
  
  <pre><code> var container = builder.Build(); // build the Autofac container and do what you will
  
   Mapper.Initialize(cfg =&gt; {
        cfg.ConstructServicesUsing(container.Resolve); // nope nope nope
        // configure mappings
        cfg.CreateMap&lt;int, TestEntity&gt;().ConvertUsing&lt;EntityConverter&lt;TestEntity&gt;&gt;()
  });
  Mapper.AssertConfigurationIsValid();
  </code></pre>
  
  <p>So here's the part that sucks. I am to understand Automapper requires the <code>ConstructServicesUsing</code> guy to be set before you build your config. If you set it later, it won't be used. The example above won't work because <code>container</code> is the root scope. If I try and resolve <code>EntityConverter&lt;TestEntity&gt;</code>, Autofac will complain that the requested type is registered for a different scope, and the one you're in ain't it. Makes sense, I want the scope created by WebApi.</p>
  
  <p>Let me pause a sec and cover one fact about WebApi dependency injection (I don't really think this is Autofac-specific). WebApi creates an <code>IDependencyScope</code> for the request, and stashes it in the <code>HttpRequestMessage.Properties</code>. I can't get it back again unless I have access to that same <code>HttpRequestMessage</code> instance. My <code>AsInstancePerApiRequest</code> scoping on <code>IRepository</code> and my converter thus rely on that <code>IDependencyScope</code>.</p>
  
  <p>So, that's really the meat and potatoes of the problem, and I really frustrated with this difference from MVC. You can't do</p>
  
  <pre><code> cfg.ConstructServicesUsing(GlobalConfiguration.Configuration.DependencyResolver.GetService);
  </code></pre>
  
  <p>That's equivalent to using <code>container.Resolve</code>. I can't use </p>
  
  <pre><code> GlobalConfiguration.Configuration.DependencyResolver.BeginScope().GetService
  </code></pre>
  
  <p>because A) that creates a new scope next to the one I actually want B) doesn't really let me clean up the new scope I created. Using Service Locator is a new way to have the same problem; I can't get to the scope WebApi is using. If my converter and its dependencies were single instance or instance per dependency, it wouldn't be a problem, but they aren't, so it is, and changing that would create lots more problems for me.</p>
  
  <p>Now, I can create AutoMapper config with Autofac and register it as a single instance. I can even create per-request <code>IMappingEngine</code> instances. But that doesn't do me any good if the service constructor always uses that single delegate you register at the beginning, which has no access to the current scope. If I could change that delegate per each mapping engine instance, I might be in business. But I can't.</p>
  
  <p>So what <em>can</em> I do?</p>