title: This resolve operation has already ended. Autofac, Automapper & IMemberValueResolver
body: <p>I have a problem with Autofac and Automapper. </p>
  
  <p>In my WebApi I have this code to register my classes:</p>
  
  <pre><code>    public static void Initialize(HttpConfiguration config, IContainer container)
          {
              config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
              _container = container;
          }
  
          private static IContainer RegisterServices(ContainerBuilder builder)
          {
              //Register your Web API controllers.  
              builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
  
              builder.RegisterModule&lt;MapperInstaller&gt;();
              builder.RegisterType&lt;VurpEntities&gt;();
              builder.RegisterType&lt;EntityFrameworkUnitOfWork&gt;().As&lt;IUnitOfWork&gt;();
  
              builder.RegisterType&lt;AppService&gt;().As&lt;IAppService&gt;();
              builder.RegisterType&lt;AppRepository&gt;().As&lt;IAppRepository&gt;();
  
              builder.RegisterType&lt;BusinessService&gt;().As&lt;IBusinessService&gt;();
              builder.RegisterType&lt;BusinessRepository&gt;().As&lt;IBusinessRepository&gt;();
  
  //...
  //all my types are registered here
  //...
  
  
  
              //Set the dependency resolver to be Autofac.  
              _container = builder.Build();
  
              return _container;
          }
  </code></pre>
  
  <p>I created a Resolver to map a special field of my object:</p>
  
  <pre><code>public class ProductTypeResolver : IMemberValueResolver&lt;ProductDto, Product, string, Enumeration.ProductType&gt;
      {
          public ProductType Resolve(ProductDto source, Product destination, string sourceMember, ProductType destMember, ResolutionContext context)
          {
              Enum.TryParse(sourceMember, out destMember);
  
              return destMember;
          }
      }
  </code></pre>
  
  <p>I have a Profile that map DTO object in Entity object:</p>
  
  <pre><code>CreateMap&lt;ProductDto, Product&gt;()
              .ForMember(dest =&gt; dest.Category, opt =&gt; opt.Ignore())
              .ForMember(dest =&gt; dest.Feature, opt =&gt; opt.Ignore())
  opts.ResolveUsing&lt;ProductTypeResolver, string&gt;(src =&gt; src.type));
  </code></pre>
  
  <p>When I try to map the object <code>Product p = _mapper.Map&lt;Product&gt;(productDto);</code> I receive the error: </p>
  
  <blockquote>
    <p>An exception of type 'System.ObjectDisposedException' occurred in
    Autofac.dll but was not handled in user code</p>
    
    <p>Additional information: This resolve operation has already ended. When
    registering components using lambdas, the IComponentContext 'c'
    parameter to the lambda cannot be stored. Instead, either resolve
    IComponentContext again from 'c', or resolve a Func&lt;> based factory to
    create subsequent components from.</p>
  </blockquote>
  
  <p>Anyone can help me?</p>