Files
Umbraco-CMS/src/Umbraco.Web/Composing/ModuleInjector.cs
Paul Johnson ec66990e72 NetCore: MSDI refactor phase 2 (#9280)
* Moved adapters from Infra -> Core

* Allow Composition to accept a service collection instead of an IRegister

* Composition no longer takes IRegister as constructor arg

all tests passing

* Composition no longer implements IRegister

* Lose _uniques in Composition

* lose Composition OnCreatingFactory actions

* Clean up UmbracoCoreServiceCollectionExtensions & Composition

Less IFactory

* LightInject gone where TFW == netstandard2.0 || TFW == netcoreapp3.1

* Resolve dead code issues

* Rename IFactory methods to match IServiceProvider so they can be trivially swapped later.

* Rename IFactory methods to match IServiceProvider so they can be trivially swapped later (continued)

Thought the counts were low, it's mostly extension method usage
2020-10-27 11:53:01 +01:00

55 lines
1.6 KiB
C#

using System.Web;
using Umbraco.Core;
using Umbraco.Core.Composing;
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
where TModule : class, IHttpModule
{
protected TModule Module { get; private set; }
/// <inheritdoc />
public void Init(HttpApplication context)
{
try
{
// using the service locator here - no other way, really
Module = Current.Factory.GetRequiredService<TModule>();
}
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<IRuntimeState>();
}
catch { /* don't make it worse */ }
if (runtimeState?.BootFailedException != null)
BootFailedException.Rethrow(runtimeState.BootFailedException);
// else... throw what we have
throw;
}
// initialize
Module.Init(context);
}
/// <inheritdoc />
public void Dispose()
{
Module?.Dispose();
}
}
}