如何在SimpleInjector中注册我的AutoMapper类?
这是我的类的一部分,其中注册Mapper SimpleInjector:
container.RegisterSingleton(Mapper.Configuration);
container.Register<IMapper>(**--What should I put here?--**)
这是我的MapperConfig类:
public class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
return new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DomainToViewModelMappingProfile());
cfg.AddProfile(new ViewModelToDomainMappingProfile());
});
}
}
答案 0 :(得分:1)
将您的automapperConfig类更改为静态类..像这样:
public static class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
return new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DomainToViewModelMappingProfile());
cfg.AddProfile(new ViewModelToDomainMappingProfile());
});
}
}
然后在App_start中的GlobalAsax.cs文件中调用它,如下所示:
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
// Register Mapping Configuration on Start up
AutoMapperConfiguration.Configure();
}
protected void Application_End()
{
//Cleanup all resources
}
}