diff --git a/src/Orchard/Environment/DefaultOrchardHost.cs b/src/Orchard/Environment/DefaultOrchardHost.cs index 195c54ef0..073c9130b 100644 --- a/src/Orchard/Environment/DefaultOrchardHost.cs +++ b/src/Orchard/Environment/DefaultOrchardHost.cs @@ -38,7 +38,7 @@ namespace Orchard.Environment { IExtensionLoaderCoordinator extensionLoaderCoordinator, IExtensionMonitoringCoordinator extensionMonitoringCoordinator, ICacheManager cacheManager, - IHostLocalRestart hostLocalRestart ) { + IHostLocalRestart hostLocalRestart) { _shellSettingsManager = shellSettingsManager; _shellContextFactory = shellContextFactory; _runningShellTable = runningShellTable; @@ -90,8 +90,9 @@ namespace Orchard.Environment { MonitorExtensions(); BuildCurrent(); + var shellContext = CreateShellContext(shellSettings); - return shellContext.LifetimeScope.CreateWorkContextScope(); + return new StandaloneEnvironmentWorkContextScopeWrapper(shellContext.LifetimeScope.CreateWorkContextScope(), shellContext); } /// @@ -153,17 +154,17 @@ namespace Orchard.Environment { /// Starts a Shell and registers its settings in RunningShellTable /// private void ActivateShell(ShellContext context) { - Logger.Debug("Activating context for tenant {0}", context.Settings.Name); + Logger.Debug("Activating context for tenant {0}", context.Settings.Name); context.Shell.Activate(); _shellContexts = (_shellContexts ?? Enumerable.Empty()) .Where(c => c.Settings.Name != context.Settings.Name) .Concat(new[] { context }) - .ToArray(); - + .ToArray(); + _runningShellTable.Add(context.Settings); } - + /// /// Creates a transient shell for the default tenant's setup /// @@ -326,5 +327,33 @@ namespace Orchard.Environment { Logger.Debug("Adding tenant to restart: " + tenant); _tenantsToRestart.GetState().Add(context.Settings); } + + // To be used from CreateStandaloneEnvironment(), also disposes the ShellContext LifetimeScope. + private class StandaloneEnvironmentWorkContextScopeWrapper : IWorkContextScope { + private readonly ShellContext _shellContext; + private readonly IWorkContextScope _workContextScope; + + public WorkContext WorkContext { + get { return _workContextScope.WorkContext; } + } + + public StandaloneEnvironmentWorkContextScopeWrapper(IWorkContextScope workContextScope, ShellContext shellContext) { + _workContextScope = workContextScope; + _shellContext = shellContext; + } + + public TService Resolve() { + return _workContextScope.Resolve(); + } + + public bool TryResolve(out TService service) { + return _workContextScope.TryResolve(out service); + } + + public void Dispose() { + _workContextScope.Dispose(); + _shellContext.LifetimeScope.Dispose(); + } + } } }