Autofac,ASP.NET MVC 3 HttpRequest的范围和AutoMapper:不带标签匹配“的HttpRequest”范围可见
当我用从automapper映射autofac注册的网络类型,我得到这个错误:Autofac,ASP.NET MVC 3 HttpRequest的范围和AutoMapper:不带标签匹配“的HttpRequest”范围可见
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.
当其他类型在其映射中起作用时被解析。 当网络类型从控制器中解析出来后,它就可以工作。
为什么不能在我的映射中成功解析web(或任何其他httprequest作用域?)类型?
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
{
}
编辑:它很容易创建一个空MVC项目,粘贴代码,并尝试一下,看看自己。
编辑:删除了ConstructServicesUsing调用,因为它不是示例所要求的。在本例中没有通过AutoMapper解决任何服务。
我想你应该在
Mapper.Initialize(c =>
{
c.ConstructServicesUsing(DependencyResolver.Current);
profiles.ToList().ForEach(c.AddProfile);
});
@rene_r使用DependencyResolver.Current.Resolve代替container.Resolve以上是在正确的轨道上;适应他的回答:
c.ConstructServicesUsing(t => DependencyResolver.Current.GetService(t))
仍然可能不会编译,但应该让你关闭。
的要求是调用DependencyResolver.Current
将被推迟到服务请求(不保持当映射器被初始化由Current
返回的值。)
我最近也有类似的问题,它横空出世在我的引导程序功能中是一个糟糕的设置。以下autofac设置为我做了。
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();
我没有在Mapper.Initialize()函数来指定解析器。刚刚叫
Mapper.Initialize(x =>
{
x.AddProfile<DomainToDTOMappingProfile>();
});
引导后,它适用于我很好。
这并不甚至编译,但它改变到\t \t \t \t \t \t \t \t \t c.ConstructServicesUsing(DependencyResolver.Current.GetService);它编译,但它仍然是同样的问题。 – MatteS