public void TenantSpecificRegistrations_shouldBeCorrect()
        {
            //setup serviceCollection
            var masterServiceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            foreach (var tenant in _tenants)
            {
                if (tenant.Id == 1)
                {
                    masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(SpecialFooService), ServiceLifetime.Scoped);
                }
                else
                {
                    masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(DefaultFooService), ServiceLifetime.Scoped);
                }
            }

            var sut = new ContainerBuilder(_tenants, masterServiceCollection);
            var tenantContainers = sut.Build();

            using (var scope1 = tenantContainers[1].CreateScope())
            {
                var tenant1FooService = scope1.ServiceProvider.GetService <IFooService>();
                Assert.IsType <SpecialFooService>(tenant1FooService);
            }

            using (var scope2 = tenantContainers[2].CreateScope())
            {
                var tenant2FooService = scope2.ServiceProvider.GetService <IFooService>();
                Assert.IsType <DefaultFooService>(tenant2FooService);
            }
        }
예제 #2
0
        public async Task InvokeCall_succeeds()
        {
            var mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            var mockHttpRequest         = new Mock <HttpRequest>();

            mockHttpRequest.Setup(_ => _.Headers)
            .Returns(new HeaderDictionary {
                { "tenantId", "1" }
            });
            mockHttpContextAccessor.Setup(x => x.HttpContext.Request)
            .Returns(mockHttpRequest.Object);

            //setup serviceCollection
            var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            foreach (var tenant in _tenants)
            {
                if (tenant.Id == 1)
                {
                    serviceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(DefaultFooService), ServiceLifetime.Scoped);
                }
                else
                {
                    serviceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(SpecialFooService), ServiceLifetime.Scoped);
                }

                //this singleton is tenant scoped.
                //there will be a singleton for each tenant
                serviceCollection.AddTenantScoped(tenant, typeof(IBarService), typeof(BarService), ServiceLifetime.Singleton);
            }

            serviceCollection.AddSingleton(_ => mockHttpContextAccessor);
            serviceCollection.AddSingleton <ITenantProvider, MockTenantProvider>();
            serviceCollection.AddSingleton <ITenantResolutionStrategy, HeaderTenantResolutionStrategy>();
            serviceCollection.AddSingleton <IBazService, BazService>();
            serviceCollection.AddSingleton(mockHttpContextAccessor.Object);

            mockHttpContextAccessor.Setup(x => x.HttpContext.RequestServices)
            .Returns(serviceCollection.BuildServiceProvider());

            string foo = null;
            var    sut = new TenantMiddleware(next: (innerHttpContext) =>
            {
                //do something in next request delegate
                foo = "executed";
                return(Task.CompletedTask);
            },
                                              new ContainerBuilder(_tenants, serviceCollection));

            await sut.Invoke(mockHttpContextAccessor.Object.HttpContext);

            Assert.Equal("executed", foo);
        }
예제 #3
0
        public async Task TenantSpecificServices_ShouldBeInjected()
        {
            //setup IHttpContextAccessor
            var mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            var mockHttpRequest         = new Mock <HttpRequest>();

            mockHttpRequest.Setup(_ => _.Headers)
            .Returns(new HeaderDictionary {
                { "tenantId", "2" }
            });
            mockHttpContextAccessor.Setup(x => x.HttpContext.Request)
            .Returns(mockHttpRequest.Object);

            //setup serviceCollection
            var masterServiceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            foreach (var tenant in _tenants)
            {
                if (tenant.Id == 1)
                {
                    masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(SpecialFooService), ServiceLifetime.Scoped);
                }
                else
                {
                    masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(DefaultFooService), ServiceLifetime.Scoped);
                }

                //this singleton is tenant scoped.
                //there will be a singleton for each tenant
                masterServiceCollection.AddTenantScoped(tenant, typeof(IBarService), typeof(BarService), ServiceLifetime.Singleton);
            }

            masterServiceCollection.AddSingleton <ITenantProvider, MockTenantProvider>();
            masterServiceCollection.AddSingleton <ITenantResolutionStrategy, HeaderTenantResolutionStrategy>();
            masterServiceCollection.AddSingleton <IBazService, BazService>();
            masterServiceCollection.AddSingleton(mockHttpContextAccessor.Object);

            mockHttpContextAccessor.Setup(x => x.HttpContext.RequestServices)
            .Returns(masterServiceCollection.BuildServiceProvider());

            var sut = new TenantMiddleware(next: (innerHttpContext) =>
            {
                var fooService = innerHttpContext.RequestServices.GetService <IFooService>();
                Assert.NotNull(fooService);
                Assert.IsType <DefaultFooService>(fooService);

                return(Task.CompletedTask);
            },
                                           new ContainerBuilder(_tenants, masterServiceCollection));

            await sut.Invoke(mockHttpContextAccessor.Object.HttpContext);
        }
        public void TenantScopedSingletonRegistrationsForDifferentTenant_shouldBeTheSameAcrossScopes()
        {
            //setup serviceCollection
            var masterServiceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            foreach (var tenant in _tenants)
            {
                //this singleton is tenant scoped. There will be a singleton for each tenant
                masterServiceCollection.AddTenantScoped(tenant, typeof(IBarService), typeof(BarService), ServiceLifetime.Singleton);
            }

            //masterServiceCollection.AddSingleton<IBazService, BazService>();

            var sut = new ContainerBuilder(_tenants, masterServiceCollection);
            var tenantContainers = sut.Build();

            IBarService tenant1BarService1, tenant2BarService1;

            using (var scope1 = tenantContainers[1].CreateScope())
            {
                tenant1BarService1 = scope1.ServiceProvider.GetService <IBarService>();
            }

            using (var scope2 = tenantContainers[2].CreateScope())
            {
                tenant2BarService1 = scope2.ServiceProvider.GetService <IBarService>();
            }

            Assert.NotEqual(tenant1BarService1.Id, tenant2BarService1.Id);
        }
        public void TenantScopedLifetime_shouldbeSameAs_scoped()
        {
            var masterServiceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            foreach (var tenant in _tenants)
            {
                if (tenant.Id == 1)
                {
                    masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(SpecialFooService), ServiceLifetime.Scoped);
                }
                else
                {
                    masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(DefaultFooService), ServiceLifetime.Scoped);
                }
            }

            var sut = new ContainerBuilder(_tenants, masterServiceCollection);
            var tenantContainers = sut.Build();

            IFooService tenant1FooService1, tenant1FooService2;

            using (var scope1 = tenantContainers[1].CreateScope())
            {
                tenant1FooService1 = scope1.ServiceProvider.GetService <IFooService>();
                tenant1FooService2 = scope1.ServiceProvider.GetService <IFooService>();
                Assert.IsType <SpecialFooService>(tenant1FooService1);
                Assert.IsType <SpecialFooService>(tenant1FooService2);
                //Same scope, should be same instance
                Assert.Equal(tenant1FooService1.Id, tenant1FooService2.Id);
            }
            using (var scope2 = tenantContainers[1].CreateScope())
            {
                var tenant1FooService3 = scope2.ServiceProvider.GetService <IFooService>();
                //Different scope, should be different instance
                Assert.NotEqual(tenant1FooService1.Id, tenant1FooService3.Id);
            }
        }
        public void TenantTransientLifetime_shouldbeDifferentWithinSameScope()
        {
            var masterServiceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            var tenant = _tenants[0];

            masterServiceCollection.AddTenantScoped(tenant, typeof(IFooService), typeof(SpecialFooService), ServiceLifetime.Transient);

            var sut = new ContainerBuilder(_tenants, masterServiceCollection);
            var tenantContainers = sut.Build();

            IFooService tenant1FooService1, tenant1FooService2;

            using (var scope1 = tenantContainers[1].CreateScope())
            {
                tenant1FooService1 = scope1.ServiceProvider.GetService <IFooService>();
                tenant1FooService2 = scope1.ServiceProvider.GetService <IFooService>();
                Assert.IsType <SpecialFooService>(tenant1FooService1);
                Assert.IsType <SpecialFooService>(tenant1FooService2);
                //Same scope, should be different instance
                Assert.NotEqual(tenant1FooService1.Id, tenant1FooService2.Id);
            }
        }