refactors where the container is created

This commit is contained in:
Shannon
2015-01-21 13:21:36 +11:00
parent 13a4d5c81c
commit 9710e6f9e2
3 changed files with 46 additions and 85 deletions

View File

@@ -36,7 +36,7 @@ namespace Umbraco.Core
/// </remarks>
public class CoreBootManager : IBootManager
{
internal ServiceContainer Container { get; private set; }
private ServiceContainer _appStartupEvtContainer;
protected ProfilingLogger ProfilingLogger { get; private set; }
private DisposableTimer _timer;
@@ -55,13 +55,17 @@ namespace Umbraco.Core
get { return _umbracoApplication; }
}
internal ServiceContainer Container
{
get { return _umbracoApplication.Container; }
}
protected IServiceProvider ServiceProvider { get; private set; }
public CoreBootManager(UmbracoApplicationBase umbracoApplication)
{
if (umbracoApplication == null) throw new ArgumentNullException("umbracoApplication");
_umbracoApplication = umbracoApplication;
Container = new ServiceContainer();
_umbracoApplication = umbracoApplication;
}
public virtual IBootManager Initialize()
@@ -70,11 +74,10 @@ namespace Umbraco.Core
throw new InvalidOperationException("The boot manager has already been initialized");
//Create logger/profiler, and their resolvers, these are special resolvers that can be resolved before frozen so we can start logging
var logger = CreateLogger();
LoggerResolver.Current = new LoggerResolver(logger) {CanResolveBeforeFrozen = true};
LoggerResolver.Current = new LoggerResolver(_umbracoApplication.Logger) { CanResolveBeforeFrozen = true };
var profiler = CreateProfiler();
ProfilerResolver.Current = new ProfilerResolver(profiler) {CanResolveBeforeFrozen = true};
ProfilingLogger = new ProfilingLogger(logger, profiler);
ProfilingLogger = new ProfilingLogger(_umbracoApplication.Logger, profiler);
_timer = ProfilingLogger.DebugDuration<CoreBootManager>("Umbraco application starting", "Umbraco application startup complete");
@@ -85,8 +88,8 @@ namespace Umbraco.Core
ServiceProvider = new ActivatorServiceProvider();
PluginManager.Current = PluginManager = new PluginManager(ServiceProvider, _cacheHelper.RuntimeCache, ProfilingLogger, true);
//build up IoC
Container = BuildContainer();
//build up core IoC servoces
ConfigureCoreServices(Container);
//set the singleton resolved from the core container
ApplicationContext.Current = ApplicationContext = Container.GetInstance<ApplicationContext>();
@@ -103,8 +106,10 @@ namespace Umbraco.Core
_appStartupEvtContainer = Container.CreateChildContainer();
_appStartupEvtContainer.BeginScope();
_appStartupEvtContainer.RegisterCollection<IApplicationEventHandler, PerScopeLifetime>(PluginManager.ResolveApplicationStartupHandlers());
//build up standard IoC services
ConfigureServices(Container);
InitializeResolvers();
InitializeModelMappers();
@@ -119,15 +124,14 @@ namespace Umbraco.Core
/// <summary>
/// Build the core container which contains all core things requird to build an app context
/// </summary>
private ServiceContainer BuildContainer()
private void ConfigureCoreServices(ServiceContainer container)
{
var container = new ServiceContainer();
container.Register<ILogger>(factory => _umbracoApplication.Logger, new PerContainerLifetime());
container.Register<IProfiler>(factory => ProfilingLogger.Profiler, new PerContainerLifetime());
container.Register<ProfilingLogger>(factory => ProfilingLogger, new PerContainerLifetime());
container.Register<IUmbracoSettingsSection>(factory => UmbracoConfig.For.UmbracoSettings());
container.Register<CacheHelper>(factory => _cacheHelper, new PerContainerLifetime());
container.Register<IRuntimeCacheProvider>(factory => _cacheHelper.RuntimeCache, new PerContainerLifetime());
container.Register<ILogger>(factory => ProfilingLogger.Logger, new PerContainerLifetime());
container.Register<IProfiler>(factory => ProfilingLogger.Profiler, new PerContainerLifetime());
container.Register<ProfilingLogger>(factory => ProfilingLogger, new PerContainerLifetime());
container.Register<IServiceProvider, ActivatorServiceProvider>();
container.Register<PluginManager>(factory => PluginManager, new PerContainerLifetime());
container.Register<IDatabaseFactory>(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance<ILogger>()));
@@ -145,8 +149,6 @@ namespace Umbraco.Core
factory.GetInstance<CacheHelper>(),
factory.GetInstance<ILogger>()));
container.Register<ApplicationContext>(new PerContainerLifetime());
return container;
}
/// <summary>
@@ -208,14 +210,6 @@ namespace Umbraco.Core
});
}
/// <summary>
/// Creates the application's ILogger
/// </summary>
protected virtual ILogger CreateLogger()
{
return Logger.CreateWithDefaultLog4NetConfiguration();
}
/// <summary>
/// Creates the application's IProfiler
/// </summary>

View File

@@ -5,8 +5,10 @@ using System.Web.Hosting;
using System.Web.Mvc;
using StackExchange.Profiling;
using Umbraco.Core.Configuration;
using Umbraco.Core.LightInject;
using Umbraco.Core.Logging;
using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Logging;
namespace Umbraco.Core
{
@@ -21,6 +23,22 @@ namespace Umbraco.Core
public abstract class UmbracoApplicationBase : System.Web.HttpApplication
{
/// <summary>
/// Umbraco application's IoC container
/// </summary>
internal ServiceContainer Container { get; private set; }
private ILogger _logger;
/// <summary>
/// Constructor
/// </summary>
protected UmbracoApplicationBase()
{
//create the container for the application, the boot managers are responsible for registrations
Container = new ServiceContainer();
}
public event EventHandler ApplicationStarting;
public event EventHandler ApplicationStarted;
@@ -40,17 +58,11 @@ namespace Umbraco.Core
/// </summary>
internal void StartApplication(object sender, EventArgs e)
{
//don't output the MVC version header (security)
MvcHandler.DisableMvcResponseHeader = true;
//boot up the application
GetBootManager()
.Initialize()
.Startup(appContext => OnApplicationStarting(sender, e))
.Complete(appContext => OnApplicationStarted(sender, e));
//And now we can dispose of our startup handlers - save some memory
//ApplicationEventsResolver.Current.Dispose();
}
/// <summary>
@@ -112,7 +124,7 @@ namespace Umbraco.Core
/// <param name="e"></param>
protected virtual void OnApplicationError(object sender, EventArgs e)
{
EventHandler handler = ApplicationError;
var handler = ApplicationError;
if (handler != null) handler(this, EventArgs.Empty);
}
@@ -141,7 +153,7 @@ namespace Umbraco.Core
/// <param name="e"></param>
protected virtual void OnApplicationEnd(object sender, EventArgs e)
{
EventHandler handler = ApplicationEnd;
var handler = ApplicationEnd;
if (handler != null) handler(this, EventArgs.Empty);
}
@@ -156,61 +168,13 @@ namespace Umbraco.Core
protected abstract IBootManager GetBootManager();
protected ILogger Logger
/// <summary>
/// Returns the logger instance for the application - this will be used throughout the entire app
/// </summary>
public virtual ILogger Logger
{
get
{
if (LoggerResolver.HasCurrent && LoggerResolver.Current.HasValue)
{
return LoggerResolver.Current.Logger;
}
return new HttpTraceLogger();
}
get { return _logger ?? (_logger = Logging.Logger.CreateWithDefaultLog4NetConfiguration()); }
}
private class HttpTraceLogger : ILogger
{
public void Error(Type callingType, string message, Exception exception)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Warn(callingType.ToString(), message + Environment.NewLine + exception);
}
public void Warn(Type callingType, string message, params Func<object>[] formatItems)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Warn(callingType.ToString(), string.Format(message, formatItems.Select(x => x())));
}
public void WarnWithException(Type callingType, string message, Exception e, params Func<object>[] formatItems)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Warn(callingType.ToString(), string.Format(message + Environment.NewLine + e, formatItems.Select(x => x())));
}
public void Info(Type callingType, Func<string> generateMessage)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Write(callingType.ToString(), generateMessage());
}
public void Info(Type type, string generateMessageFormat, params Func<object>[] formatItems)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Write(type.ToString(), string.Format(generateMessageFormat, formatItems.Select(x => x())));
}
public void Debug(Type callingType, Func<string> generateMessage)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Write(callingType.ToString(), generateMessage());
}
public void Debug(Type type, string generateMessageFormat, params Func<object>[] formatItems)
{
if (HttpContext.Current == null) return;
HttpContext.Current.Trace.Write(type.ToString(), string.Format(generateMessageFormat, formatItems.Select(x => x())));
}
}
}
}

View File

@@ -413,6 +413,9 @@ namespace Umbraco.Web
/// </summary>
private void SetupMvcAndWebApi()
{
//don't output the MVC version header (security)
MvcHandler.DisableMvcResponseHeader = true;
//set master controller factory
ControllerBuilder.Current.SetControllerFactory(
new MasterControllerFactory(FilteredControllerFactoriesResolver.Current));