When I use a web type registered with autofac from an automaton map, I get this error:
No area with a tag matching "httpRequest" is visible from the area in which the instance was requested. This usually indicates that a component registered as an HTTP request is requested by the SingleInstance () component (or a similar script). As part of web integration, dependencies on DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime are always requested, never from the container itself.
When a different type is allowed in the mapping, it works. When the web type is enabled from the controller, it works.
Why can't the web (or any other types of URLs bound to a URL?) Fail to resolve successfully in my mapping?
protected void Application_Start() { var builder = new ContainerBuilder(); builder.RegisterModule<AutofacWebTypesModule>(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterModelBinders(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .AssignableTo<Profile>() .As<Profile>() ; builder.Register(c => Mapper.Engine) .As<IMappingEngine>(); builder.RegisterType<AnotherType>() .As<IAnotherType>(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); var profiles = container.Resolve<IEnumerable<Profile>>(); Mapper.Initialize(c => profiles.ToList().ForEach(c.AddProfile)); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } public class HomeController : Controller { private readonly IMappingEngine _mapper; private readonly Func<HttpContextBase> _httpContext; public HomeController(IMappingEngine mapper, Func<HttpContextBase> httpContext) { _mapper = mapper; _httpContext = httpContext; } public ActionResult Index() { var test = _httpContext.Invoke(); return View(_mapper.Map<Model, ViewModel>(new Model())); } } public class MyProfile : Profile { private readonly Func<HttpContextBase> _httpContext; private readonly Func<IAnotherType> _anotherType; public MyProfile(Func<HttpContextBase> httpContext, Func<IAnotherType> anotherType) { _httpContext = httpContext; _anotherType = anotherType; } protected override void Configure() { CreateMap<Model, ViewModel>() .ForMember(d => d.Url, o => o.ResolveUsing(s => { 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 { }
EDIT : its easy to create an empty MVC project, paste the code and try it and see for yourself.
EDIT : Removed the ConstructServicesUsing call because it is not needed in this example. No services are allowed using AutoMapper in this example.
source share