Files
Umbraco-CMS/src/Umbraco.Web/Composing/ModuleInjector.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2019-01-09 17:39:32 +01:00
using System.Web;
2018-07-20 09:49:05 +02:00
using Umbraco.Core;
2018-07-20 16:39:39 +02:00
using Umbraco.Core.Composing;
2018-10-25 15:07:04 +02:00
using Umbraco.Core.Exceptions;
namespace Umbraco.Web.Composing
{
/// <summary>
/// Provides a base class for module injectors.
/// </summary>
/// <typeparam name="TModule">The type of the injected module.</typeparam>
public abstract class ModuleInjector<TModule> : IHttpModule
2019-01-09 17:39:32 +01:00
where TModule : class, IHttpModule
{
protected TModule Module { get; private set; }
/// <inheritdoc />
public void Init(HttpApplication context)
{
2018-09-02 22:09:37 +02:00
try
{
// using the service locator here - no other way, really
2018-11-28 11:05:41 +01:00
Module = Current.Factory.GetInstance<TModule>();
2018-09-02 22:09:37 +02:00
}
catch
{
2018-10-25 15:07:04 +02:00
// if GetInstance fails, it may be because of a boot error, in
// which case that is the error we actually want to report
IRuntimeState runtimeState = null;
try
2018-09-02 22:09:37 +02:00
{
2018-11-28 11:05:41 +01:00
runtimeState = Current.Factory.GetInstance<IRuntimeState>();
2018-09-02 22:09:37 +02:00
}
2018-10-25 15:07:04 +02:00
catch { /* don't make it worse */ }
if (runtimeState?.BootFailedException != null)
BootFailedException.Rethrow(runtimeState.BootFailedException);
// else... throw what we have
throw;
2018-09-02 22:09:37 +02:00
}
2018-10-25 15:07:04 +02:00
// initialize
Module.Init(context);
}
/// <inheritdoc />
public void Dispose()
{
Module?.Dispose();
}
}
}