using System.Web;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Exceptions;
namespace Umbraco.Web.Composing
{
///
/// Provides a base class for module injectors.
///
/// The type of the injected module.
public abstract class ModuleInjector : IHttpModule
where TModule : class, IHttpModule
{
protected TModule Module { get; private set; }
///
public void Init(HttpApplication context)
{
try
{
// using the service locator here - no other way, really
Module = Current.Factory.GetRequiredService();
}
catch
{
// 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
{
runtimeState = Current.Factory.GetRequiredService();
}
catch { /* don't make it worse */ }
if (runtimeState?.BootFailedException != null)
BootFailedException.Rethrow(runtimeState.BootFailedException);
// else... throw what we have
throw;
}
// initialize
Module.Init(context);
}
///
public void Dispose()
{
Module?.Dispose();
}
}
}