// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add services to the collection. Don't build or return // any IServiceProvider or the ConfigureContainer method // won't get called. //services.AddAutofac(); services.AddOptions( ); // This adds the required middleware to the ROOT CONTAINER and is required for multitenancy to work. //AddAutofacMultitenantRequestServices(); //AutofacChildScopeServiceProviderFactory(); // AutofacChildLifetimeScopeConfigurationAdapter(); var adapter = new AutofacChildLifetimeScopeConfigurationAdapter(); var actions = new AutofacChildLifetimeScopeConfigurationAdapter(); adapter.Add(builder => { }); actions.Add(builder => builder.Populate(services)); //AutofacContainer = services.BuildServiceProvider().GetAutofacRoot(); // Singleton BuildServiceProvider var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var factory2 = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScopeWithDependency <IPrintMessages>(typeof(IPrintMessages))); var myServices = new ServiceCollection().AddTransient <IPrintMessages>(); services.AddSingleton <IPrintMessages>( ); var configurationAdapter = factory.CreateBuilder(services); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); var builder = new ContainerBuilder(); foreach (var action in configurationAdapter.ConfigurationActions) { action(builder); } configurationAdapter.Add(builder => builder.RegisterType <IPrintMessages>( )); //var service = serviceProvider.GetRequiredService<IPrintMessages>(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores <ApplicationDbContext>(); services.AddControllersWithViews(); services.AddRazorPages(); // Add services to the collection. Don't build or return // any IServiceProvider or the ConfigureContainer method // won't get called. //services.AddAutofac(); services.AddOptions(); // This adds the required middleware to the ROOT CONTAINER and is required for multitenancy to work. //AddAutofacMultitenantRequestServices(); //AutofacChildScopeServiceProviderFactory(); //AutofacChildLifetimeScopeConfigurationAdapter(); var factory = new AutofacChildLifetimeScopeServiceProviderFactory(AutofacContainer); var configurationAdapter = factory.CreateBuilder(services); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); var builder = new ContainerBuilder(); foreach (var action in configurationAdapter.ConfigurationActions) { action(builder); } }
public void CreateServiceProviderReturnsAutofacServiceProvider() { var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var serviceProvider = factory.CreateServiceProvider(new AutofacChildLifetimeScopeConfigurationAdapter()); Assert.IsType <AutofacServiceProvider>(serviceProvider); }
public void CreateServiceProviderThrowsWhenProvidedNullAdapter() { var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var exception = Assert.Throws <ArgumentNullException>(() => factory.CreateServiceProvider(null)); Assert.Equal("configurationAdapter", exception.ParamName); }
public void CreateServiceProviderBuildsServiceProviderUsingAdapter() { var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var services = new ServiceCollection().AddTransient <object>(); var configurationAdapter = factory.CreateBuilder(services); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); Assert.NotNull(serviceProvider.GetService(typeof(object))); }
public void CreateServiceProviderAddDepToRootContainerResolveable() { var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScopeWithDependency <DependencyOne>(typeof(DependencyOne))); var configurationAdapter = factory.CreateBuilder(new ServiceCollection()); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); serviceProvider.GetRequiredService <DependencyOne>(); }
public void CreateServiceProviderAddDepToServiceCollectionAndAddConfigurationTypesResolveable() { var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var services = new ServiceCollection().AddTransient <DependencyOne>(); var configurationAdapter = factory.CreateBuilder(services); configurationAdapter.Add(builder => builder.RegisterType <DependencyTwo>()); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); serviceProvider.GetRequiredService <DependencyOne>(); serviceProvider.GetRequiredService <DependencyTwo>(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores <ApplicationDbContext>(); services.AddControllersWithViews(); services.AddRazorPages(); // Add services to the collection. Don't build or return // any IServiceProvider or the ConfigureContainer method // won't get called. //services.AddAutofac(); services.AddOptions(); //services.AddSingleton<IServiceProviderFactory<ContainerBuilder>>(new AutofacServiceProviderFactory()); // This adds the required middleware to the ROOT CONTAINER and is required for multitenancy to work. //AddAutofacMultitenantRequestServices(); //AutofacChildScopeServiceProviderFactory(); // AutofacChildLifetimeScopeConfigurationAdapter(); var adapter = new AutofacChildLifetimeScopeConfigurationAdapter(); var actions = new AutofacChildLifetimeScopeConfigurationAdapter(); adapter.Add(builder => { }); actions.Add(builder => builder.Populate(services)); //AutofacContainer = services.BuildServiceProvider().GetAutofacRoot(); // Singleton BuildServiceProvider var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var factory2 = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScopeWithDependency <IPrintMessages>(typeof(IPrintMessages))); var myServices = new ServiceCollection().AddTransient <IPrintMessages>(); services.AddSingleton <IPrintMessages>(); var configurationAdapter = factory.CreateBuilder(services); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); var builder = new ContainerBuilder(); foreach (var action in configurationAdapter.ConfigurationActions) { action(builder); } configurationAdapter.Add(builder => builder.RegisterType <IPrintMessages>()); //var service = serviceProvider.GetRequiredService<IPrintMessages>(); }
public void ConfigureServices(IServiceCollection services) { var adapter = new AutofacChildLifetimeScopeConfigurationAdapter(); var actions = new AutofacChildLifetimeScopeConfigurationAdapter(); adapter.Add(builder => { }); actions.Add(builder => builder.Populate(services)); var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope); var factory2 = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScopeWithDependency <IPrintMessages>(typeof(IPrintMessages))); var myServices = new ServiceCollection().AddTransient <IPrintMessages>(); services.AddSingleton <IPrintMessages>(); var configurationAdapter = factory.CreateBuilder(services); var serviceProvider = factory.CreateServiceProvider(configurationAdapter); var builder = new ContainerBuilder(); foreach (var action in configurationAdapter.ConfigurationActions) { action(builder); } configurationAdapter.Add(builder => builder.RegisterType <IPrintMessages>()); }