2016-03-09 44 views
0

我在我的應用程序中使用最新版本的Automapper和Autofac。我設置了配置和配置文件,並且單元測試了我的所有配置文件AssertIsConfigurationValid(),並且一切正常。 然而,當我用我的應用程序中的映射 我得到一個"Automapper missing type map configuration or unsupported mapping"例外,只是從代碼中運行時,我懷疑這是值得做的怎麼設置映射器與Autofac工作:設置Autofac與Automapper配合使用的問題

// This is how I register my mapper with Autofac 
    public class ModelsMapperModule : Module 
    { 
     protected override void Load(ContainerBuilder builder) 
     { 
      builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>(); 

      builder.Register(c => new MapperConfiguration(cfg => 
      { 
       foreach (var profile in c.Resolve<IEnumerable<Profile>>()) 
       { 
        cfg.AddProfile(profile); 
       } 
      })).AsSelf().SingleInstance(); 

      builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope(); 
     } 
    } 


// Here is a simple version of my objects and mappings: 

public class LetterDomain 
{ 
    public List<StationDomain> Stations {get; set;} 

    public string Title {get; set;} 

    public int Id {get; set;} 

    public int TimeCreated {get; set;} 

    public string File {get; set;} 

    public bool IsSecret {get; set;} 
} 

public class StationDomain 
{ 
    public int Id {get; set;} 

    public string Owner {get; set;} 

    public string Name {get; set;} 
} 

public class LetterDto 
{ 
    public DestinationDto Dest {get; set;} 

    public int Id {get; set;} 
} 

public class DestinationDto 
{ 
    public List<StationDto> Stations {get; set;} 
} 

public class StationDto 
{ 
    public string Name {get; set;} 
} 

public class MyProfile : Profile 
{ 
    protected override void Configure 
    { 
     CreateMap<StationDomain, StationDto>() 

     CreateMap<LetterDomain, DestinationDto>(); 

     CreateMap<LetterDomain, LetterDto>() 
      .ForMember(x => x.Dest, opt => opt.MapFrom(src => Mapper.Map<DestinationDto>(src))); 
    } 
} 


public void MyMethodInsideApplication(LetterDomain letter) 
{ 
    // Exception is thrown here 
    var dto = _mapper.Map<LetterDto>(letter); 
} 

我米試圖映射LetterDomain在我的應用程序LetterDto,它告訴我的LetterDomainDestinationDto配置丟失,但我絕對創建的映射..

真的想在這裏有些幫助..

提前致謝!

編輯: 我只想補充一點,這並不在應用程序中使用靜態Mapper.Map <>的配置文件工作的良好內的所有其他映射

+0

在哪個班沒有了'MyMethodInsideApplication'法活嗎?這樣的類是通過容器創建的嗎?你能說明一下嗎? –

+0

這是一個單例,它在startapp中解析 –

+0

你在哪裏使用靜態的'Mapper.Map <>'方法?你可以顯示該代碼嗎? –

回答

0

的一個問題是,你沒有具體說明當從LetterDomain映射到DestinationDto時應該填充DestinationDto.Name

換句話說,LetterDomain沒有Name屬性,所以沒有自動映射是可能的該屬性。

+0

我的不好,貼錯了..現在修好了。我敢肯定,所有的地圖都很好..我在單元測試中試過它,它很好..它必須與我的應用程序代碼有關 - 特別是在註冊insance映射器後使用靜態映射器 –

+0

我認爲它可能有自從您確認驗證成功以來,您一直是您的發帖。也許這個答案會有所幫助:http://stackoverflow.com/questions/35565524/cannot-resolve-automapper-imapper-using-automapper-4-2-with-autofac/35566443#35566443 – devuxer

+0

儘管註冊與Autofac似乎沒關係,因爲我確實在應用程序的配置圖不使用靜態Mapper.Map <>方法的其他部分中成功使用映射器。 無論如何,是否有理由同時註冊IMapper和IMappingEngine? –

0

我想你不能同時使用靜態和非靜態版本的映射器(除非你創建靜態和非靜態映射,但是沒用);順便說一下,你不需要使用

CreateMap<LetterDomain, LetterDto>() 
     .ForMember(x => x.Dest, opt => opt.MapFrom(src => Mapper.Map<DestinationDto>(src))); 

應用映射,如果存在只是

CreateMap<LetterDomain, LetterDto>() 
     .ForMember(x => x.Dest, opt => opt.MapFrom(src)); 

的地圖將會自動應用。

您還可以簡化映射過程一點,如果你想:

protected override void Load(ContainerBuilder builder) 
    { 
     var profiles = 
      from t in typeof (MapperModuleRegistration).Assembly.GetTypes() 
      where typeof (Profile).IsAssignableFrom(t) 
      select (Profile) Activator.CreateInstance(t); 

     var config = new MapperConfiguration(cfg => 
     { 
      foreach (var profile in profiles) 
      { 
       cfg.AddProfile(profile); 
      } 
     }); 

     builder.RegisterInstance(config).As<MapperConfiguration>(); 
     builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper()).As<IMapper>(); 
    } 

希望它能幫助:)