Autofac, ASP.NET MVC 3 area httpRequest and AutoMapper: there is no visible area with a label corresponding to "httpRequest"

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.

+2
source share
3 answers

@rene_r above is on the right track; adapting his answer:

 c.ConstructServicesUsing(t => DependencyResolver.Current.GetService(t)) 

It may still not compile, but should close you.

The requirement is that the call to DependencyResolver.Current deferred until a service is requested (not saved as the value returned by Current when the converter is initialized.)

+2
source

I think you should use DependencyResolver.Current.Resolve instead of container.Resolve in

 Mapper.Initialize(c => { c.ConstructServicesUsing(DependencyResolver.Current); profiles.ToList().ForEach(c.AddProfile); }); 
0
source

I recently had a similar problem and this turned out to be a bad setup in my bootstrapper function. The following autofac setup did this for me.

 builder.Register(c => new ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers)) .AsImplementedInterfaces() .SingleInstance(); builder.Register(c => Mapper.Engine) .As<IMappingEngine>() .SingleInstance(); builder.RegisterType<TypeMapFactory>() .As<ITypeMapFactory>() .SingleInstance(); 

I did not need to specify resolver in the Mapper.Initialize () function. Just called

 Mapper.Initialize(x => { x.AddProfile<DomainToDTOMappingProfile>(); }); 

after the boot, and it works great for me.

0
source

Source: https://habr.com/ru/post/1436155/


All Articles