title: Autofac, ASP.NET MVC 3 httpRequest scope and AutoMapper: No scope with a Tag matching 'httpRequest' is visible
body: <p>When I use a web type registered with autofac from an automapper mapping, I get this error:</p>
  
  <blockquote>
    <p>No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.</p>
  </blockquote>
  
  <p>When another type is resolved in the mapping it works.
  When a web type is resolved from the controller it works.</p>
  
  <p>Why doesnt web (or any other httprequest scoped?) types get successfully resolved in my mapping?</p>
  
  <pre><code>    protected void Application_Start()
      {
          var builder = new ContainerBuilder();
          builder.RegisterModule&lt;AutofacWebTypesModule&gt;();
          builder.RegisterControllers(Assembly.GetExecutingAssembly());
          builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
          builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
              .AssignableTo&lt;Profile&gt;()
              .As&lt;Profile&gt;()
              ;
          builder.Register(c =&gt; Mapper.Engine)
              .As&lt;IMappingEngine&gt;();
          builder.RegisterType&lt;AnotherType&gt;()
              .As&lt;IAnotherType&gt;();
          var container = builder.Build();
  
          DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  
          var profiles = container.Resolve&lt;IEnumerable&lt;Profile&gt;&gt;();
          Mapper.Initialize(c =&gt; profiles.ToList().ForEach(c.AddProfile));
  
          AreaRegistration.RegisterAllAreas();
  
          RegisterGlobalFilters(GlobalFilters.Filters);
          RegisterRoutes(RouteTable.Routes);
      }
  
  public class HomeController : Controller
  {
      private readonly IMappingEngine _mapper;
      private readonly Func&lt;HttpContextBase&gt; _httpContext;
  
      public HomeController(IMappingEngine mapper, Func&lt;HttpContextBase&gt; httpContext)
      {
          _mapper = mapper;
          _httpContext = httpContext;
      }
  
      public ActionResult Index()
      {
          var test = _httpContext.Invoke();
          return View(_mapper.Map&lt;Model, ViewModel&gt;(new Model()));
      }
  
  }
  
  public class MyProfile : Profile
  {
      private readonly Func&lt;HttpContextBase&gt; _httpContext;
      private readonly Func&lt;IAnotherType&gt; _anotherType;
  
      public MyProfile(Func&lt;HttpContextBase&gt; httpContext, Func&lt;IAnotherType&gt; anotherType)
      {
          _httpContext = httpContext;
          _anotherType = anotherType;
      }
  
      protected override void Configure()
      {
          CreateMap&lt;Model, ViewModel&gt;()
              .ForMember(d =&gt; d.Url, o =&gt; o.ResolveUsing(s =&gt;
                                                      {
                                                          var test = _anotherType.Invoke().GetAValue();
                                                          return _httpContext.Invoke().Request.Url;
                                                      }))
              ;
      }
  }
  
  public interface IAnotherType
  {
      string GetAValue();
  }
  
  public class AnotherType : IAnotherType
  {
      public string GetAValue() { return "a value"; }
  }
  
  public class ViewModel
  {
      public string Url { get; set; }
  }
  
  public class Model
  {
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: Its easy to create an empty MVC project, paste the code and try it out and see for yourself.</p>
  
  <p><strong>EDIT</strong>: Removed the ConstructServicesUsing call because its not required by the example. No services are resolved through AutoMapper in the example.</p>