Example #1
0
        public void CreateServiceCollection_ResolvesSingletons_FromApplicationServices_ForClosedTypes_AsEnumerable()
        {
            // Arrange
            var service1 = new Service <string>();
            var service2 = new Service <string>();

            var services = new ServiceCollection();

            services.AddSingleton <IService <string> >(service1);
            services.AddSingleton <IService <string> >(service2);
            var provider = services.BuildServiceProvider();
            var factory  = new TenantServiceCollectionFactory(provider, services);

            // Act
            var tenantServices       = factory.CreateServiceCollection();
            var applicationSingleton = provider.GetService <IService <string> >();

            // Assert
            var descriptor = tenantServices.SingleOrDefault(sd => sd.ServiceType == typeof(IEnumerable <IService <string> >));

            Assert.NotNull(descriptor);
            Assert.NotNull(descriptor.ImplementationInstance);
            Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);

            var enumerable = (IEnumerable <IService <string> >)descriptor.ImplementationInstance;

            Assert.Equal(2, enumerable.Count());
        }
        private TenantServiceProviderResolver CreateResolver()
        {
            var services = new ServiceCollection();
            var provider = services.BuildServiceProvider();
            var factory  = new TenantServiceCollectionFactory(provider, services);
            var resolver = new TenantServiceProviderResolver(factory);

            return(resolver);
        }
Example #3
0
        public void CreateServiceCollection_CreatesNewServiceCollection()
        {
            // Arrange
            var services = new ServiceCollection();
            var provider = services.BuildServiceProvider();
            var factory  = new TenantServiceCollectionFactory(provider, services);

            // Act
            var tenantServices = factory.CreateServiceCollection();

            // Assert
            Assert.NotNull(tenantServices);
            Assert.NotSame(services, tenantServices);
        }
Example #4
0
        public void CreateServiceCollection_ConsumesScopedServiceDescriptors()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddScoped <IService <string>, Service <string> >();
            var provider = services.BuildServiceProvider();
            var factory  = new TenantServiceCollectionFactory(provider, services);

            // Act
            var tenantServices = factory.CreateServiceCollection();

            // Assert
            var descriptor = tenantServices.SingleOrDefault(sd => sd.ServiceType == typeof(IService <string>));

            Assert.NotNull(descriptor);
            Assert.Equal(typeof(Service <string>), descriptor.ImplementationType);
            Assert.Equal(ServiceLifetime.Scoped, descriptor.Lifetime);
        }
Example #5
0
        public void CreateServiceCollection_ReplacesSelfReferencingServiceCollection()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddSingleton <IServiceCollection>(services);
            var provider = services.BuildServiceProvider();
            var factory  = new TenantServiceCollectionFactory(provider, services);

            // Act
            var tenantServices = factory.CreateServiceCollection();

            // Assert
            var descriptor = tenantServices.SingleOrDefault(sd => sd.ServiceType == typeof(IServiceCollection));

            Assert.NotNull(descriptor);
            Assert.Same(tenantServices, descriptor.ImplementationInstance);
            Assert.NotSame(services, descriptor.ImplementationInstance);
        }
Example #6
0
        public void CreateServiceCollection_TenantScopedServices_ResolvedPerTenantServiceProvider()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddTenantScoped <IService <string>, Service <string> >();
            var provider = services.BuildServiceProvider();
            var factory  = new TenantServiceCollectionFactory(provider, services);

            // Act
            var tenantServiceProvider1 = factory.CreateServiceCollection().BuildServiceProvider();
            var tenantServiceProvider2 = factory.CreateServiceCollection().BuildServiceProvider();

            // Assert
            var service1 = tenantServiceProvider1.GetService <IService <string> >();
            var service2 = tenantServiceProvider2.GetService <IService <string> >();

            Assert.NotSame(service1, service2);
        }
        /// <summary>
        /// Initialises a new instance of <see cref="TenantServiceProviderResolver"/>
        /// </summary>
        /// <param name="services">The tenant-scoped services</param>
        public TenantServiceProviderResolver(TenantServiceCollectionFactory serviceCollectionFactory)
        {
            Ensure.IsNotNull(serviceCollectionFactory, nameof(serviceCollectionFactory));

            _tenantServices = new Lazy <IServiceCollection>(() => serviceCollectionFactory.CreateServiceCollection(), LazyThreadSafetyMode.PublicationOnly);
        }