2017-10-09 123 views
0

我正在開發一個.NET MVC 5應用程序與實體框架6. 我已經創建了我的數據庫。然後我做了一個簡單的ViewModel並將其用於HomeController.But當我嘗試映射與控制器數據庫模型視圖模型我得到這個錯誤:使用AutoMapper時激活IConfigurationProvider錯誤

Error activating IConfigurationProvider No matching bindings are available, and the type is not self-bindable. Activation path: 3) Injection of dependency IConfigurationProvider into parameter configurationProvider of constructor of type Mapper 2) Injection of dependency IMapper into parameter mapper of constructor of type HomeController 1) Request for HomeController

Suggestions: 1) Ensure that you have defined a binding for IConfigurationProvider. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.

這是我的HomeController:

public ActionResult Index() 
    { 
     var ads = this.adService 
      .GetAll() 
      .ToList() 
      .Select(x => this.mapper.Map<AdViewModel>(x)); 


     var viewModel = new MainViewModel() 
     { 
      Ads = ads, 
     }; 

     return View(viewModel); 

     //return View(); 
    } 

這是我AutoMapperConfiguration:

public class AutoMapperConfig 
{ 
    public static IMapperConfigurationExpression Configuration { get; private set; } 

    public void Execute(Assembly assembly) 
    { 
     Mapper.Initialize(
      cfg => 
      { 
       var types = assembly.GetExportedTypes(); 
       LoadStandardMappings(types, cfg); 
       LoadCustomMappings(types, cfg); 
       Configuration = cfg; 
      }); 
    } 

    private static void LoadStandardMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration) 
    { 
     var maps = (from t in types 
        from i in t.GetInterfaces() 
        where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>) && 
          !t.IsAbstract && 
          !t.IsInterface 
        select new 
        { 
         Source = i.GetGenericArguments()[0], 
         Destination = t 
        }).ToArray(); 

     foreach (var map in maps) 
     { 
      mapperConfiguration.CreateMap(map.Source, map.Destination); 
      mapperConfiguration.CreateMap(map.Destination, map.Source); 
     } 
    } 

    private static void LoadCustomMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration) 
    { 
     var maps = (from t in types 
        from i in t.GetInterfaces() 
        where typeof(IHaveCustomMappings).IsAssignableFrom(t) && 
          !t.IsAbstract && 
          !t.IsInterface 
        select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray(); 

     foreach (var map in maps) 
     { 
      map.CreateMappings(mapperConfiguration); 
     } 
    } 
} 

}

的NinjectConfig:

private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind(x => 
     { 
      x.FromThisAssembly() 
      .SelectAllClasses() 
      .BindDefaultInterface(); 
     }); 

     kernel.Bind(x => 
     { 
      x.FromAssemblyContaining(typeof(IService)) 
      .SelectAllClasses() 
      .BindDefaultInterface(); 
     }); 

     kernel.Bind(typeof(IEfRepository<>)).To(typeof(EfRepository<>)); 
     kernel.Bind(typeof(DbContext), typeof(MsSqlDbContext)).To<MsSqlDbContext>().InRequestScope(); 
     kernel.Bind<ISaveContext>().To<SaveContext>(); 
     kernel.Bind<IMapper>().To<Mapper>(); 

    } 

而且在Global.asax:

protected void Application_Start() 
    { 
     Database.SetInitializer(new MigrateDatabaseToLatestVersion<MsSqlDbContext, Configuration>()); 

     AreaRegistration.RegisterAllAreas(); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 

     var mapper = new AutoMapperConfig(); 
     mapper.Execute(Assembly.GetExecutingAssembly()); 
    } 

我期待着看到一個想法解決這個錯誤,以及如何注入這個IConfigurationProvider?

回答

0

而不是試圖解決IConfigurationProvider依賴項,我可以建議將IMapper映射到已經初始化的Mapper實例的方法。 自動映射器初始化代碼現在將在Ninject配置時執行,而不是在Global.asax中執行。是

我建議變動如下:

AutoMapperConfig.cs:

public static class AutoMapperConfig 
{ 
    public static IMapperConfigurationExpression Configuration { get; private set; } 
    public static IMapper MapperInstance { get; private set; } 

    static AutoMapperConfig() 
    { 
     Mapper.Initialize(cfg => 
     { 
      var types = Assembly.GetExecutingAssembly().GetExportedTypes(); 
      LoadStandardMappings(types, cfg); 
      LoadCustomMappings(types, cfg); 
      Configuration = cfg; 
     }); 

     MapperInstance = Mapper.Instance; 
    } 

    private static void LoadStandardMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration) 
    { 
     var maps = (from t in types 
        from i in t.GetInterfaces() 
        where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>) && 
          !t.IsAbstract && 
          !t.IsInterface 
        select new 
        { 
         Source = i.GetGenericArguments()[0], 
         Destination = t 
        }).ToArray(); 

     foreach (var map in maps) 
     { 
      mapperConfiguration.CreateMap(map.Source, map.Destination); 
      mapperConfiguration.CreateMap(map.Destination, map.Source); 
     } 
    } 

    private static void LoadCustomMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration) 
    { 
     var maps = (from t in types 
        from i in t.GetInterfaces() 
        where typeof(IHaveCustomMappings).IsAssignableFrom(t) && 
          !t.IsAbstract && 
          !t.IsInterface 
        select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray(); 

     foreach (var map in maps) 
     { 
      map.CreateMappings(mapperConfiguration); 
     } 
    } 
} 

NinjectConfig:

private static void RegisterServices(IKernel kernel) 
{ 
    kernel.Bind(x => 
    { 
     x.FromThisAssembly() 
     .SelectAllClasses() 
     .BindDefaultInterface(); 
    }); 

    kernel.Bind(x => 
    { 
     x.FromAssemblyContaining(typeof(IService)) 
     .SelectAllClasses() 
     .BindDefaultInterface(); 
    }); 

    kernel.Bind(typeof(IEfRepository<>)).To(typeof(EfRepository<>)); 
    kernel.Bind(typeof(DbContext), typeof(MsSqlDbContext)).To<MsSqlDbContext>().InRequestScope(); 
    kernel.Bind<ISaveContext>().To<SaveContext>(); 
    kernel.Bind<IMapper>().ToMethod(ctx => AutoMapperConfig.MapperInstance); 
} 

的Global.asax:

protected void Application_Start() 
{ 
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MsSqlDbContext, Configuration>()); 

    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
}