Autofac, ASP.NET MVC 3 httpRequest scope and AutoMapper: No scope with a Tag matching 'httpRequest' is visible

让人想犯罪 __ 提交于 2019-12-06 05:37:08

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

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

Still might not compile but should get you close.

The requirement is that the call to DependencyResolver.Current is deferred until the service is requested (not kept as the value returned by Current when the mapper was initialised.)

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);
                 });
Fabricio Martínez Tamayo

I recently had a similar problem and it turned out to be a bad setup in my bootstrapper function. The following autofac setup did it 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 have to specify resolver in the Mapper.Initialize() function. Just called

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

after the bootstrapped and it works fine for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!