本文介绍了注入的ISession到自定义valueresolver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图注入的Isession 的一个实例到自定义的 AutoMapper ValueResolver

下面是解析器

 公共类ContactTypeResolver
    :ValueResolver< Common.Models.ContactType,Models.ContactType>
{
    ISession的_session;    公共ContactTypeResolver(ISession的会话)
    {
        _session =会议;
    }    保护覆盖Models.ContactType ResolveCore(Common.Models.ContactType源)
    {
        返回_session.Load< Models.ContactType>(source.Id);
    }
}

我对设立AutoMapper配置文件

  this.CreateMap< Models.PhoneNumber,Common.Models.PhoneNumber>()
    .ReverseMap()
    .ForMember(D => d.Type,O => o.ResolveUsing< ContactTypeResolver>());

我喜欢所以StructureMap登记处登记的解析器

 对少于ValueResolver< Common.Models.ContactType,Models.ContactType>>()
   。新增< ContactTypeResolver>();

我使用的session-per-请求,我设置会话嵌套容器内部的内 StructureMapDependencyScope.cs 当我加入StructureMap向其中创建了网页API项目。这里的code

 公共无效CreateNestedContainer()
{
    如果(CurrentNestedContainer!= NULL)
    {
        返回;
    }
    CurrentNestedContainer = Container.GetNestedContainer();
    CurrentNestedContainer.Configure(C => c.For< ISession的方式>()使用(CTX => ctx.GetInstance< ISessionFactory>()的openSession()));}

这是我的容器是如何设置的。

  VAR容器= StructuremapMvc.StructureMapDependencyScope.Container;
GlobalConfiguration.Configuration.DependencyResolver =新StructureMapWebApiDependencyResolver(容器);container.Configure(X =>
{
    x.IncludeRegistry< D​​efaultRegistry>();
});Mapper.Initialize(CFG =>
{
    cfg.ConstructServicesUsing(container.GetInstance);    的foreach()(在container.GetAllInstances&LT VAR个人资料;资料&GT)
        cfg.AddProfile(配置文件);
});

我甚至尝试使用的服务定位器像这样

  this.CreateMap< Models.PhoneNumber,Common.Models.PhoneNumber>()
    .ReverseMap()
    .ForMember(D => d.Type,O =≠0
         .ResolveUsing< ContactTypeResolver>()
         .ConstructedBy(StructureMap.ObjectFactory.GetInstance&所述; ContactTypeResolver&GT));

然而,当code运行时我得到一个运行时异常说明

I also tried injecting another type registered in my container, which also did not work. What am I missing? The session instance does get injected into other objects. Also, other mappings that don't require dependency injection in the same profile work just fine.

解决方案

I believe @RadimKöhler is right.

Your parent container (where the AutoMapper types are configured) doesn't have access to the plugin types defined in the nested container. It just doesn't know about the ISession plugin type hence the exception you get.

I can think of a couple solutions here. DISCLAIMER I didn't tested them.

  1. Move the registration of the ISession type to your parent container. You should be able to scope it to HTTP requests there as well.

For example with the following registration code. I have no knowledge about ASP.NET WebAPI so there might be some differences but you get the point.

public class NHibernateRegistry : Registry
{
      public NHibernateRegistry()
      {
        For<Configuration>().Singleton().Use(c => new ConfigurationFactory().AssembleConfiguration());
        For<ISessionFactory>().Singleton().Use(c => c.GetInstance<Configuration>().BuildSessionFactory());
        For<ISession>().HybridHttpOrThreadLocalScoped().Use(c =>
        {
          var sessionFactory = c.GetInstance<ISessionFactory>();
          return sessionFactory.OpenSession();
        });
      }
    }
  1. Tell AutoMapper you want to use the nested container.

这篇关于注入的ISession到自定义valueresolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 23:50