本文介绍了Autofac,ASP.NET MVC 3 HTT prequest范围和AutoMapper:一个标签匹配'HTT prequest“没有范围是可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用从automapper映射autofac注册的网络类型,我得到这个错误:

When I use a web type registered with autofac from an automapper mapping, I get this error:

没有一个标签匹配'HTT prequest'的范围是其中要求该实例的范围可见。这通常表示登记为每个HTTP请求的组件是由一个SingleInstance()成分reqested(或类似的情形。)在幅积分总是请求从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime依赖关系,不会从容器本身

在另一种类型是在它工作的映射解决。
当网络类型是从它的工作原理控制器解决。

When another type is resolved in the mapping it works.When a web type is resolved from the controller it works.

为什么犯规网页(或任何其他HTT prequest范围?)类型得到我的映射成功解决?

Why doesnt web (or any other httprequest scoped?) types get successfully resolved 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
{
}

修改:它很容易创建一个空MVC项目,粘贴code,并尝试一下,看看自己

EDIT: Its easy to create an empty MVC project, paste the code and try it out and see for yourself.

修改:删除了ConstructServicesUsing调用,因为它不是由例子必需的。没有服务是通过AutoMapper在本例中得到解决。

EDIT: Removed the ConstructServicesUsing call because its not required by the example. No services are resolved through AutoMapper in the example.

推荐答案

@rene_r上面是正确的轨道上;适应他的回答:

@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.

的要求是调用直到服务请求(不保留由的返回值电流 DependencyResolver.Current 被推迟code>当映射器被初始化。)

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.)

这篇关于Autofac,ASP.NET MVC 3 HTT prequest范围和AutoMapper:一个标签匹配'HTT prequest“没有范围是可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:57