From 4c201bcaaa2cd3b9c4e228e2eb7db1836880da10 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Tue, 12 Jun 2018 23:12:44 +0200 Subject: [PATCH] HttpModules able to get injection. UmbracoModule + rest (if implemented) gets dependencies injected through ContainerUmbracoModule. --- src/Umbraco.Web/Routing/PublishedRouter.cs | 2 +- src/Umbraco.Web/Runtime/WebRuntime.cs | 2 + src/Umbraco.Web/UmbracoModule.cs | 94 ++++++++++++++++++---- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Web/Routing/PublishedRouter.cs b/src/Umbraco.Web/Routing/PublishedRouter.cs index fdc8540aa0..8e7d786e93 100644 --- a/src/Umbraco.Web/Routing/PublishedRouter.cs +++ b/src/Umbraco.Web/Routing/PublishedRouter.cs @@ -20,7 +20,7 @@ namespace Umbraco.Web.Routing { // fixme - make this public // fixme - making sense to have an interface? - internal class PublishedRouter + public class PublishedRouter { private readonly IWebRoutingSection _webRoutingSection; private readonly ContentFinderCollection _contentFinders; diff --git a/src/Umbraco.Web/Runtime/WebRuntime.cs b/src/Umbraco.Web/Runtime/WebRuntime.cs index 78610c01b8..9d08a4d85b 100644 --- a/src/Umbraco.Web/Runtime/WebRuntime.cs +++ b/src/Umbraco.Web/Runtime/WebRuntime.cs @@ -56,6 +56,8 @@ namespace Umbraco.Web.Runtime { base.Compose(container); + container.Register(); + // replace CoreRuntime's IProfiler registration container.RegisterSingleton(_ => _webProfiler); diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index d5fb8dee92..d41ad9ccd0 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -4,8 +4,10 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Web; +using System.Web.Mvc; using System.Web.Routing; using LightInject; +using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.IO; @@ -22,8 +24,38 @@ using Umbraco.Core.Services; using Umbraco.Web.Composing; using Umbraco.Web.PublishedCache; +[assembly: PreApplicationStartMethod(typeof(Umbraco.Web.ContainerUmbracoModule), "Start")] namespace Umbraco.Web { + public class ContainerUmbracoModule : IHttpModule + { + private UmbracoModule umbracoModule; + + public static void Start() + { + DynamicModuleUtility.RegisterModule(typeof(ContainerUmbracoModule)); + } + + public ContainerUmbracoModule() + { + + } + + public void Init(HttpApplication context) + { + // Should be extended with lazy list of modules from registry + // Example here: https://haacked.com/archive/2011/06/03/dependency-injection-with-asp-net-httpmodules.aspx/ + + umbracoModule = Current.Container.GetInstance(); + umbracoModule.Init(context); + } + + public void Dispose() + { + umbracoModule.Dispose(); + } + } + // also look at IOHelper.ResolveUrlsFromTextString - nightmarish?! // context.RewritePath supports ~/ or else must begin with /vdir @@ -38,44 +70,74 @@ namespace Umbraco.Web // get our dependencies injected manually, through properties, in // Init(). works for dependencies that are singletons. - [Inject] public IUmbracoSettingsSection UmbracoSettings { get; set; } - [Inject] public IGlobalSettings GlobalSettings { get; set; } - [Inject] public IUmbracoContextAccessor UmbracoContextAccessor { get; set; } - [Inject] public IPublishedSnapshotService PublishedSnapshotService { get; set; } - [Inject] public IUserService UserService { get; set; } - [Inject] public UrlProviderCollection UrlProviders { get; set; } - [Inject] public IRuntimeState Runtime { get; set; } - [Inject] public ILogger Logger { get; set; } - [Inject] internal PublishedRouter PublishedRouter { get; set; } - [Inject] internal IUmbracoDatabaseFactory DatabaseFactory { get; set; } - [Inject] internal IVariationContextAccessor VariationContextAccessor { get; set; } - + #endregion - public UmbracoModule() + public UmbracoModule() + : this( + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance(), + Current.Container.GetInstance() + ) + { + } + + public UmbracoModule( + IUmbracoSettingsSection umbracoSettings, + IGlobalSettings globalSettings, + IUmbracoContextAccessor umbracoContextAccessor, + IPublishedSnapshotService publishedSnapshotService, + IUserService userService, + UrlProviderCollection urlProviders, + IRuntimeState runtime, + ILogger logger, + PublishedRouter publishedRouter, + IUmbracoDatabaseFactory databaseFactory, + IVariationContextAccessor variationContextAccessor + ) { _combinedRouteCollection = new Lazy(CreateRouteCollection); + + UmbracoSettings = umbracoSettings; + GlobalSettings = globalSettings; + UmbracoContextAccessor = umbracoContextAccessor; + PublishedSnapshotService = publishedSnapshotService; + UserService = userService; + UrlProviders = urlProviders; + Runtime = runtime; + Logger = logger; + PublishedRouter = publishedRouter; + DatabaseFactory = databaseFactory; + VariationContextAccessor = variationContextAccessor; } #region HttpModule event handlers @@ -543,10 +605,10 @@ namespace Umbraco.Web }; return; } - - // modules are *not* instanciated by the container so we have to + + // modules **are** instanciated by the container so we **don't** have to // get our dependencies injected manually, through properties. - Core.Composing.Current.Container.InjectProperties(this); + //Core.Composing.Current.Container.InjectProperties(this); app.BeginRequest += (sender, e) => {