public async Task <IMultiTenantContext <T>?> ResolveAsync(object context)
        {
            IMultiTenantContext <T>?result = null;

            foreach (var strategy in Strategies)
            {
                var _strategy  = new MultiTenantStrategyWrapper(strategy, loggerFactory?.CreateLogger(strategy.GetType()) ?? NullLogger.Instance);
                var identifier = await _strategy.GetIdentifierAsync(context);

                if (options.CurrentValue.IgnoredIdentifiers.Contains(identifier, StringComparer.OrdinalIgnoreCase))
                {
                    (loggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance).LogInformation("Ignored identifier: {Identifier}", identifier);
                    identifier = null;
                }

                if (identifier != null)
                {
                    foreach (var store in Stores)
                    {
                        var _store     = new MultiTenantStoreWrapper <T>(store, loggerFactory?.CreateLogger(store.GetType()) ?? NullLogger.Instance);
                        var tenantInfo = await _store.TryGetByIdentifierAsync(identifier);

                        if (tenantInfo != null)
                        {
                            result           = new MultiTenantContext <T>();
                            result.StoreInfo = new StoreInfo <T> {
                                Store = store, StoreType = store.GetType()
                            };
                            result.StrategyInfo = new StrategyInfo {
                                Strategy = strategy, StrategyType = strategy.GetType()
                            };
                            result.TenantInfo = tenantInfo;

                            await options.CurrentValue.Events.OnTenantResolved(new TenantResolvedContext { Context =
                                                                                                               context, TenantInfo = tenantInfo, StrategyType = strategy.GetType(), StoreType = store.GetType() });

                            break;
                        }
                    }

                    if (result != null)
                    {
                        break;
                    }

                    await options.CurrentValue.Events.OnTenantNotResolved(new TenantNotResolvedContext { Context = context, Identifier = identifier });
                }
            }

            return(result);
        }
示例#2
0
        public async Task <IMultiTenantContext <T> > ResolveAsync(object context)
        {
            IMultiTenantContext <T> result = null;

            foreach (var strategy in Strategies)
            {
                var _strategy  = new MultiTenantStrategyWrapper(strategy, loggerFactory?.CreateLogger(strategy.GetType()));
                var identifier = await _strategy.GetIdentifierAsync(context);

                if (options.CurrentValue.IgnoredIdentifiers.Contains(identifier))
                {
                    Utilities.TryLoginfo(loggerFactory?.CreateLogger(GetType()), $"Ignored identifier: {identifier}");
                    identifier = null;
                }

                if (identifier != null)
                {
                    foreach (var store in Stores)
                    {
                        var _store     = new MultiTenantStoreWrapper <T>(store, loggerFactory?.CreateLogger(store.GetType()));
                        var tenantInfo = await _store.TryGetByIdentifierAsync(identifier);

                        if (tenantInfo != null)
                        {
                            result           = new MultiTenantContext <T>();
                            result.StoreInfo = new StoreInfo <T> {
                                Store = store, StoreType = store.GetType()
                            };
                            result.StrategyInfo = new StrategyInfo {
                                Strategy = strategy, StrategyType = strategy.GetType()
                            };
                            result.TenantInfo = tenantInfo;

                            break;
                        }
                    }

                    if (result != null)
                    {
                        break;
                    }
                }
            }

            return(result);
        }