Exemple #1
0
        /// <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));
            }
        }
Exemple #2
0
            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;
            }