I am trying to register my profiles into the autofac from the different project. I've tried the different ways which given below, but I am not successful.
The ways which I've tried with the help of google...
//builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>();
//builder.RegisterSource(new ContravariantRegistrationSource());
//builder.RegisterAssemblyTypes(typeof(Profile).Assembly).AsImplementedInterfaces();
//builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
// .Where(t => typeof(Profile).IsAssignableFrom())
// .As<Profile>();
//builder.RegisterAssemblyTypes(typeof(AutoMapperModule).Assembly).As<Profile>();
//builder.RegisterAssemblyTypes(typeof(ServiceName).Assembly).As<Profile>();
//builder.RegisterAssemblyTypes(typeof(RepositoryName).Assembly).As<Profile>();
//builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).AssignableTo<Profile>().As<Profile>();
builder.Register(context => new MapperConfiguration(cfg =>
{
foreach (var profile in context.Resolve<IEnumerable<Profile>>())
{
cfg.AddProfile(profile);
}
})).AsSelf().SingleInstance();
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve))
.As<IMapper>()
.InstancePerLifetimeScope();
straight forward way which is working for me is
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new APIProject.APIMappingProfile());
cfg.AddProfile(new ServiceProject.ServiceMappingProfile());
cfg.AddProfile(new RepositoryProject.RepositoryMappingProfile());
//This list is keep on going...
});
var mapper = mapperConfiguration.CreateMapper();
builder.RegisterInstance(mapper).As<IMapper>();
My sample profiles
public class APIMappingProfile : Profile
{
public APIMappingProfile()
{
CreateMap<ServiceModel, APIModel>().ReverseMap();
}
}
Please suggest me a way to auto register all of my profiles into autofac.