public static AdaptedContainerBuilderOptions <TTenant> WithStructureMap <TTenant>(this ContainerBuilderOptions <TTenant> options, Action <TTenant, ConfigurationExpression> configureTenant) where TTenant : class { var adaptorFactory = new Func <ITenantContainerAdaptor>(() => { // host level container. var container = new StructureMap.Container(); container.Populate(options.Builder.Services); var hostContainerConfigurators = container.GetAllInstances <IStructureMapHostContainerConfigurator>(); if (hostContainerConfigurators != null) { foreach (var configurator in hostContainerConfigurators) { container.Configure(configurator.ConfigureHostContainer); } } var adaptedContainer = container.GetInstance <ITenantContainerAdaptor>(); // add ITenantContainerBuilder<TTenant> service to the host container // This service can be used to build a child container (adaptor) for a particular tenant, when required. container.Configure(_ => _.For <ITenantContainerBuilder <TTenant> >() .Use(new StructureMapTenantContainerBuilder <TTenant>(adaptedContainer, configureTenant)) ); var adaptor = container.GetInstance <ITenantContainerAdaptor>(); return(adaptor); }); var adapted = new AdaptedContainerBuilderOptions <TTenant>(options, adaptorFactory); options.Builder.Services.TryAddScoped((_) => adapted); return(adapted); }