/// <summary>
        /// Registers the specified shellContext as a dependency such that they are also reloaded when the current shell context is reloaded.
        /// </summary>
        public void AddDependentShell(ShellContext shellContext)
        {
            // If the dependent is released, nothing to do.
            if (shellContext.Released)
            {
                return;
            }

            // If the dependency is already released.
            if (_released)
            {
                // The dependent is released immediately.
                shellContext.Release();
                return;
            }

            lock (_synLock)
            {
                if (_dependents == null)
                {
                    _dependents = new List <WeakReference <ShellContext> >();
                }

                // Remove any previous instance that represent the same tenant in case it has been released (restarted).
                _dependents.RemoveAll(x => !x.TryGetTarget(out var shell) || shell.Settings.Name == shellContext.Settings.Name);

                _dependents.Add(new WeakReference <ShellContext>(shellContext));
            }
        }
        /// <summary>
        /// Registers the specified shellContext as a dependency such that they are also reloaded when the current shell context is reloaded.
        /// </summary>
        public void AddDependentShell(ShellContext shellContext)
        {
            lock (this)
            {
                if (_dependents == null)
                {
                    _dependents = new List <WeakReference <ShellContext> >();
                }

                // Remove any previous instance that represent the same tenant in case it has been released (restarted).
                _dependents.RemoveAll(x => !x.TryGetTarget(out var shell) || shell.Settings.Name == shellContext.Settings.Name);

                // The same item can safely be added multiple times in a Hashset
                _dependents.Add(new WeakReference <ShellContext>(shellContext));
            }
        }
            public ServiceScopeWrapper(ShellContext shellContext)
            {
                // Prevent the context from being released until the end of the scope
                Interlocked.Increment(ref shellContext._refCount);

                _shellContext   = shellContext;
                _serviceScope   = shellContext.ServiceProvider.CreateScope();
                ServiceProvider = _serviceScope.ServiceProvider;

                var httpContextAccessor = ServiceProvider.GetRequiredService <IHttpContextAccessor>();

                if (httpContextAccessor.HttpContext == null)
                {
                    httpContextAccessor.HttpContext = new DefaultHttpContext();
                }

                _httpContext                 = httpContextAccessor.HttpContext;
                _existingServices            = _httpContext.RequestServices;
                _httpContext.RequestServices = ServiceProvider;
            }
            public ServiceScopeWrapper(ShellContext shellContext)
            {
                // Prevent the context from being disposed until the end of the scope
                Interlocked.Increment(ref shellContext._refCount);

                _shellContext = shellContext;

                // The service provider is null if we try to create
                // a scope on a disabled shell or already disposed.
                if (_shellContext.ServiceProvider == null)
                {
                    throw new ArgumentNullException(nameof(shellContext.ServiceProvider), $"Can't resolve a scope on tenant: {shellContext.Settings.Name}");
                }

                _serviceScope   = shellContext.ServiceProvider.CreateScope();
                ServiceProvider = _serviceScope.ServiceProvider;

                var httpContextAccessor = ServiceProvider.GetRequiredService <IHttpContextAccessor>();

                _httpContext                 = httpContextAccessor.HttpContext;
                _existingServices            = _httpContext.RequestServices;
                _httpContext.RequestServices = ServiceProvider;
            }