public HomeController(IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy, IMultitenantService standardService, IMetadataConsumer metadataService)
 {
     this.Dependency = dependency;
     this.TenantIdentificationStrategy = tenantIdStrategy;
     this.StandardServiceProxy = standardService;
     this.MetadataServiceProxy = metadataService;
 }
Пример #2
0
 public HomeController(IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy, IMultitenantService standardService, IMetadataConsumer metadataService)
 {
     this.Dependency = dependency;
     this.TenantIdentificationStrategy = tenantIdStrategy;
     this.StandardServiceProxy         = standardService;
     this.MetadataServiceProxy         = metadataService;
 }
Пример #3
0
        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterType <TenantService>().As <ITenantService>().SingleInstance();
            builder.RegisterType <TenantResolver>().As <ITenantResolver>().SingleInstance();
            builder.RegisterType <MyTenantIdentificationStrategy>().As <ITenantIdentificationStrategy>().SingleInstance();
            builder.RegisterType <DbUsingService>().As <IDbUsingService>();
            builder.RegisterType <FirstTenantTestService>().Keyed <ITestService>(nameof(FirstTenantTestService));
            builder.RegisterType <SecondTenantTestService>().Keyed <ITestService>(nameof(SecondTenantTestService));

            IServiceCollection tenantServices = new ServiceCollection();

            tenantServices.AddDbContext <ApplicationDbContext>();

            builder.Populate(tenantServices);

            builder.Register <Tenant>(container =>
            {
                ITenantIdentificationStrategy strategy = container.Resolve <ITenantIdentificationStrategy>();
                strategy.TryIdentifyTenant(out object id);
                if (id != null)
                {
                    if (container.IsRegistered(typeof(ITenantResolver)))
                    {
                        var tenantResolver = container.Resolve <ITenantResolver>();
                        return(tenantResolver.ResolveAsync(id).Result);
                    }
                }
                return(new Tenant());
            }).InstancePerLifetimeScope();
        }
Пример #4
0
 public TenantActionSelector(
     IActionDescriptorCollectionProvider actionDescriptorCollectionProvider,
     ActionConstraintCache actionConstraintCache,
     ILoggerFactory loggerFactory, ITenantIdentificationStrategy tenantIdentificationStrategy) : base(actionDescriptorCollectionProvider, actionConstraintCache, loggerFactory)
 {
     _tenantIdentificationStrategy = tenantIdentificationStrategy;
 }
Пример #5
0
 public TenantAccessorDependency(ITenantIdentificationStrategy tenantIdentificationStrategy)
 {
     if (tenantIdentificationStrategy.TryIdentifyTenant(out var tenantId) &&
         tenantId is string currentTenant)
     {
         CurrentTenant = currentTenant;
     }
 }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="AutofacContrib.Multitenant.Wcf.TenantPropagationMessageInspector{TTenantId}"/> class.
 /// </summary>
 /// <param name="tenantIdentificationStrategy">
 /// The strategy to use for identifying the current tenant.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="tenantIdentificationStrategy" /> is <see langword="null" />.
 /// </exception>
 public TenantPropagationMessageInspector(ITenantIdentificationStrategy tenantIdentificationStrategy)
 {
     if (tenantIdentificationStrategy == null)
     {
         throw new ArgumentNullException("tenantIdentificationStrategy");
     }
     this.TenantIdentificationStrategy = tenantIdentificationStrategy;
 }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="TenantPropagationBehavior{TTenantId}"/> class.
        /// </summary>
        /// <param name="tenantIdentificationStrategy">
        /// The strategy to use for identifying the current tenant.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="tenantIdentificationStrategy" /> is <see langword="null" />.
        /// </exception>
        public TenantPropagationBehavior(ITenantIdentificationStrategy tenantIdentificationStrategy)
        {
            if (tenantIdentificationStrategy == null)
            {
                throw new ArgumentNullException(nameof(tenantIdentificationStrategy));
            }

            this.TenantIdentificationStrategy = tenantIdentificationStrategy;
        }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Autofac.Extras.Multitenant.MultitenantContainer"/> class.
 /// </summary>
 /// <param name="tenantIdentificationStrategy">
 /// The strategy to use for identifying the current tenant.
 /// </param>
 /// <param name="applicationContainer">
 /// The application container from which tenant-specific lifetimes will
 /// be created.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="tenantIdentificationStrategy" /> or
 /// <paramref name="applicationContainer"/> is <see langword="null" />.
 /// </exception>
 public MultitenantContainer(ITenantIdentificationStrategy tenantIdentificationStrategy, IContainer applicationContainer)
 {
     if (tenantIdentificationStrategy == null)
     {
         throw new ArgumentNullException("tenantIdentificationStrategy");
     }
     if (applicationContainer == null)
     {
         throw new ArgumentNullException("applicationContainer");
     }
     this.TenantIdentificationStrategy = tenantIdentificationStrategy;
     this.ApplicationContainer         = applicationContainer;
 }
Пример #9
0
        /// <summary>
        /// Gets a typed tenant ID from a strategy or the default value for the type
        /// if identification fails.
        /// </summary>
        /// <typeparam name="T">The type of the tenant ID.</typeparam>
        /// <param name="strategy">
        /// The <see cref="Autofac.Extras.Multitenant.ITenantIdentificationStrategy"/> from which the tenant ID should be retrieved.
        /// </param>
        /// <returns>
        /// If tenant identification succeeds, the ID from <paramref name="strategy" /> is converted to
        /// <typeparamref name="T"/> and returned. If identification fails, the default value for
        /// <typeparamref name="T"/> is returned.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="strategy" /> is <see langword="null" />.
        /// </exception>
        public static T IdentifyTenant <T>(this ITenantIdentificationStrategy strategy)
        {
            if (strategy == null)
            {
                throw new ArgumentNullException("strategy");
            }
            object id;

            if (strategy.TryIdentifyTenant(out id))
            {
                return((T)id);
            }
            return(default(T));
        }
Пример #10
0
        /// <summary>
        /// Builds a service info response.
        /// </summary>
        /// <param name="serviceImplementation">
        /// The service implementation that will be returning the response.
        /// </param>
        /// <param name="dependency">
        /// The dependency that was provided to the service implementation on construction.
        /// </param>
        /// <param name="tenantIdStrategy">
        /// The tenant ID strategy.
        /// </param>
        /// <returns>
        /// A populated service info response.
        /// </returns>
        public static GetServiceInfoResponse Build(IMultitenantService serviceImplementation, IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy)
        {
            object tenantId = null;
            bool success = tenantIdStrategy.TryIdentifyTenant(out tenantId);
            if (!success || tenantId == null)
            {
                tenantId = "[Default Tenant]";
            }

            var response = new GetServiceInfoResponse()
            {
                ServiceImplementationTypeName = serviceImplementation.GetType().Name,
                DependencyInstanceId = dependency.InstanceId,
                DependencyTypeName = dependency.GetType().Name,
                TenantId = tenantId.ToString()
            };
            return response;
        }
Пример #11
0
        private static IContainer RegisterServices(ContainerBuilder builder, ITenantIdentificationStrategy idStrategy)
        {
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.Register(c => idStrategy).As <ITenantIdentificationStrategy>();
            builder.Register(c => {
                ITenantIdentificationStrategy s = c.Resolve <ITenantIdentificationStrategy>();
                object tenantId;
                s.TryIdentifyTenant(out tenantId);
                return(tenantId);
            }).Keyed <object>("tenantId");

            // creates a logger instance per tenant
            builder.RegisterType <LoggerService>()
            .As <ILoggerService>().WithParameter((pi, c) => pi.Name == "tenant",
                                                 (pi, c) => c.ResolveKeyed <object>("tenantId")).InstancePerTenant();

            MultitenantContainer mtc = new MultitenantContainer(
                idStrategy, builder.Build());

            return(mtc);
        }
Пример #12
0
        private static IContainer RegisterServices(ContainerBuilder builder, ITenantIdentificationStrategy idStrategy) {

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.Register(c => idStrategy).As<ITenantIdentificationStrategy>();
            builder.Register(c => {
                
                ITenantIdentificationStrategy s = c.Resolve<ITenantIdentificationStrategy>();
                object tenantId;
                s.TryIdentifyTenant(out tenantId);
                return tenantId;

            }).Keyed<object>("tenantId");

            // creates a logger instance per tenant
            builder.RegisterType<LoggerService>()
                .As<ILoggerService>().WithParameter((pi, c) => pi.Name == "tenant",
                    (pi, c) => c.ResolveKeyed<object>("tenantId")).InstancePerTenant();

            MultitenantContainer mtc = new MultitenantContainer(
                idStrategy, builder.Build());

            return mtc;
        }
Пример #13
0
 public MultiTenantConfigureOnDemandStartupFilter(ITenantIdentificationStrategy tenantIdentificationStrategy, MultitenantContainer multitenantContainer)
 {
     this.tenantIdentificationStrategy = tenantIdentificationStrategy;
     this.multitenantContainer         = multitenantContainer;
 }
Пример #14
0
 public Tenant2UserService(ITenantIdentificationStrategy tenantIdentificationStrategy)
     : base(tenantIdentificationStrategy)
 {
 }
Пример #15
0
 public TenantService(ITenantIdentificationStrategy tenantIdentificationStrategy, ITenantStorage <T> tenantStorage)
 {
     _tenantIdentificationStrategy = tenantIdentificationStrategy;
     _tenantStorage = tenantStorage;
 }
 public Tenant1Controller(IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy, IMultitenantService standardService, IMetadataConsumer metadataService) :
     base(dependency, tenantIdStrategy, standardService, metadataService)
 {
 }
Пример #17
0
 public TenantPropogationOutboundInterceptor(ITenantIdentificationStrategy identificationStrategy)
 {
     _identificationStrategy = identificationStrategy;
 }
Пример #18
0
        public static void Initialize(HttpConfiguration config, ITenantIdentificationStrategy idStrategy) {

            Initialize(config,
                RegisterServices(new ContainerBuilder(), idStrategy));
        }
 public HomeController(IServiceProxy service,
                       ITenantIdentificationStrategy tenantIdentificationStrategy)
     : base(service, tenantIdentificationStrategy)
 {
     _service = service;
 }
 public Tenant1Implementation(IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy)
 {
     this.Dependency = dependency;
     this.TenantIdentificationStrategy = tenantIdStrategy;
 }
Пример #21
0
 public static void Initialize(HttpConfiguration config, ITenantIdentificationStrategy idStrategy)
 {
     Initialize(config,
                RegisterServices(new ContainerBuilder(), idStrategy));
 }
Пример #22
0
 public TwitterController(IAuthentication service, ITenantIdentificationStrategy tenantIdStrategy)
 {
     this.service = service;
     this.tenantIdentificationStrategy = tenantIdStrategy;
 }
Пример #23
0
        /// <summary>
        /// Builds a service info response.
        /// </summary>
        /// <param name="serviceImplementation">
        /// The service implementation that will be returning the response.
        /// </param>
        /// <param name="dependency">
        /// The dependency that was provided to the service implementation on construction.
        /// </param>
        /// <param name="tenantIdStrategy">
        /// The tenant ID strategy.
        /// </param>
        /// <returns>
        /// A populated service info response.
        /// </returns>
        public static GetServiceInfoResponse Build(IMultitenantService serviceImplementation, IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy)
        {
            var tenantId = (object)null;
            var success  = tenantIdStrategy.TryIdentifyTenant(out tenantId);

            if (!success || tenantId == null)
            {
                tenantId = "[Default Tenant]";
            }

            var response = new GetServiceInfoResponse()
            {
                ServiceImplementationTypeName = serviceImplementation.GetType().Name,
                DependencyInstanceId          = dependency.InstanceId,
                DependencyTypeName            = dependency.GetType().Name,
                TenantId = tenantId.ToString()
            };

            return(response);
        }
Пример #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Autofac.Extras.Multitenant.MultitenantContainer"/> class.
 /// </summary>
 /// <param name="tenantIdentificationStrategy">
 /// The strategy to use for identifying the current tenant.
 /// </param>
 /// <param name="applicationContainer">
 /// The application container from which tenant-specific lifetimes will
 /// be created.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="tenantIdentificationStrategy" /> or
 /// <paramref name="applicationContainer"/> is <see langword="null" />.
 /// </exception>
 public MultitenantContainer(ITenantIdentificationStrategy tenantIdentificationStrategy, IContainer applicationContainer)
 {
     if (tenantIdentificationStrategy == null)
     {
         throw new ArgumentNullException("tenantIdentificationStrategy");
     }
     if (applicationContainer == null)
     {
         throw new ArgumentNullException("applicationContainer");
     }
     this.TenantIdentificationStrategy = tenantIdentificationStrategy;
     this.ApplicationContainer = applicationContainer;
 }
Пример #25
0
        public void IdentifyTenant_NullStrategy()
        {
            ITenantIdentificationStrategy strategy = null;

            Assert.Throws <ArgumentNullException>(() => strategy.IdentifyTenant <Guid>());
        }
Пример #26
0
 public TenantResolutionFromTokenValidationLibrary(IHttpContextAccessor httpContextAccessor)
 {
     this.resolutionStrategy = new CookieResolutionStrategy(httpContextAccessor);
 }
 public DefaultUserService(ITenantIdentificationStrategy tenantIdentificationStrategy)
 {
     _tenantIdentificationStrategy = tenantIdentificationStrategy;
 }
Пример #28
0
 public Tenant1Controller(IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy, IMultitenantService standardService, IMetadataConsumer metadataService) :
     base(dependency, tenantIdStrategy, standardService, metadataService)
 {
 }
Пример #29
0
 public Tenant1Implementation(IDependency dependency, ITenantIdentificationStrategy tenantIdStrategy)
 {
     this.Dependency = dependency;
     this.TenantIdentificationStrategy = tenantIdStrategy;
 }
Пример #30
0
        public void ShouldBeAbleToCreateMultiTenantContainer()
        {
            string currentTenant = "test";

            var cb = new ContainerBuilder();

            // perform default registrations
            cb.RegisterInstance("none").As <string>();

            var root     = cb.Build();
            var strategy = new Mock <ITenantIdentificationStrategy>();

            // for the purpose of this test, select tenant based on the value of a local variable
            // in practice, this normally come from some ambient value (e.g. HttpContext.Current,
            // OperationContext.Current, ExcecutionContext etc)
            strategy.Setup(s => s.TryIdentifyTenant(out It.Ref <object> .IsAny))
            .Returns(new TryIdentifyTenantSetup((out object tenantId) =>
            {
                if (string.IsNullOrEmpty(currentTenant))
                {
                    tenantId = "default";
                    return(true);
                }
                tenantId = currentTenant;
                return(true);
            }
                                                )
                     );
            ITenantIdentificationStrategy tenantIdStrategy = strategy.Object;

            var multi = new MultitenantContainer(
                tenantIdStrategy,
                root

                );

            // each tenant can supply configuration overrides as necessary
            multi.ConfigureTenant("test1", (tenantCb) =>
            {
                tenantCb.RegisterInstance("tenant is test1").As <string>();
            });
            multi.ConfigureTenant("test2", (tenantCb) =>
            {
                tenantCb.RegisterInstance("tenant is test2").As <string>();
            });


            Assert.IsTrue(multi.TenantIsConfigured("test1"), @" multi.TenantIsConfigured(""test1"") ");

            // tenant 1
            currentTenant = "test1";
            using (var scope = multi.BeginLifetimeScope())
            {
                var resolved = scope.Resolve <string>();
                Assert.AreEqual("tenant is test1", resolved, @" ""tenant is test1"" vs resolved");
            }
            Assert.IsTrue(multi.TenantIsConfigured("test2"), @" multi.TenantIsConfigured(""test2"") ");
            // tenant 2
            currentTenant = "test2";
            using (var scope = multi.BeginLifetimeScope())
            {
                var resolved = scope.Resolve <string>();
                Assert.AreEqual("tenant is test2", resolved, @" ""tenant is test2"" vs resolved");
            }

            // no tenant
            currentTenant = null;
            using (var scope = multi.BeginLifetimeScope())
            {
                var resolved = scope.Resolve <string>();
                Assert.AreEqual("none", resolved, @" ""none"" vs resolved");
            }

            //unregistered tenant
            currentTenant = "not registered tenant";
            using (var scope = multi.BeginLifetimeScope())
            {
                var resolved = scope.Resolve <string>();
                Assert.AreEqual("none", resolved, @" ""none"" vs resolved");
            }
        }