I'm trying to create an autofac container that is passed a list of services to register for use in across a number of projects.
I've got the base container builder with all the required types across a number of properties, however I need to then inject a list of services from the class using this base.
what I have:
private void buildAutofacContainer(IServiceCollection services, List<ServiceDescriptor> servicesToAdd)
{
var builder = new ContainerBuilder();
builder.Register(x => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build()).As<IConfigurationRoot>().SingleInstance();
builder.RegisterType<LoggingInterceptor>();
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>();
builder.RegisterType<ApplicationEngine>().As<IApplicationEngine>();
builder.RegisterType<ApplicationSettings>().As<IApplicationSettings>();
builder.RegisterType<PortalUnitOfWorkFactory>().As<IPortalUnitOfWorkFactory>();
builder.RegisterType<DataConnectionService>().As<IDataConnectionService>();
builder.Populate(services);
this.Container = builder.Build();
}
What I'm trying to do:
servicesToAdd.ForEach(o =>
{
object concrete = o.ImplementationInstance;
Type type = o.ImplementationType;
builder.RegisterType<concrete>().As<type>();
});
any help would be greatly appreciated