Files
Umbraco-CMS/src/Umbraco.Web/Composing/ModuleInjector.cs
Paul Johnson 4ae329589a NetCore: MSDI refactor remove IFactory & IRegister (#9308)
* Replace IFactory with IServiceProvider

* Replace IRegister with IServiceCollection

* Fix Rte.cshtml so the view can service locate.

* Replace Composing Lifetime with MSDI ServiceLifetime

* Remove ServiceProvider AddMultipleUnique extension

* Remove Umbraco.Web.Composing.Current.Reset and any calls

* Remove LightInject from net framework projects

* Brought back a helper for setting MediaFileSystem underlying IFileSystem
2020-10-30 12:16:17 +01:00

56 lines
1.6 KiB
C#

using System.Web;
using Microsoft.Extensions.DependencyInjection;
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();
}
}
}