From 953c3a667ab6bd675f5876386e62faeb82353581 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 27 Feb 2020 11:48:38 +0100 Subject: [PATCH] More logic in AspNetCoreHostingEnvironment --- .../AspNetCoreHostingEnvironment.cs | 66 +++++++++++++++---- .../AspNetCoreUmbracoApplicationLifetime.cs | 10 +-- ...coBackOfficeServiceCollectionExtensions.cs | 4 +- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreHostingEnvironment.cs b/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreHostingEnvironment.cs index 2827d397a5..5cd2b590c8 100644 --- a/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreHostingEnvironment.cs +++ b/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreHostingEnvironment.cs @@ -1,29 +1,39 @@ using System; +using System.Collections.Concurrent; using System.IO; +using System.Threading; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Hosting; using Umbraco.Core; using Umbraco.Core.Configuration; - namespace Umbraco.Web.BackOffice.AspNetCore { public class AspNetCoreHostingEnvironment : Umbraco.Core.Hosting.IHostingEnvironment { + private readonly ConcurrentDictionary _registeredObjects = + new ConcurrentDictionary(); + private readonly IHostingSettings _hostingSettings; private readonly IWebHostEnvironment _webHostEnvironment; + private readonly IHttpContextAccessor _httpContextAccessor; + private readonly IHostApplicationLifetime _hostApplicationLifetime; private string _localTempPath; - public AspNetCoreHostingEnvironment(IHostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment) + public AspNetCoreHostingEnvironment(IHostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContextAccessor, IHostApplicationLifetime hostHostApplicationLifetime) { _hostingSettings = hostingSettings ?? throw new ArgumentNullException(nameof(hostingSettings)); _webHostEnvironment = webHostEnvironment; + _httpContextAccessor = httpContextAccessor; + _hostApplicationLifetime = hostHostApplicationLifetime; SiteName = webHostEnvironment.ApplicationName; ApplicationId = AppDomain.CurrentDomain.Id.ToString(); ApplicationPhysicalPath = webHostEnvironment.ContentRootPath; ApplicationVirtualPath = "/"; //TODO how to find this, This is a server thing, not application thing. - // IISVersion = HttpRuntime.IISVersion; + IISVersion = new Version(0, 0); // TODO not necessary IIS IsDebugMode = _hostingSettings.DebugMode; } public bool IsHosted { get; } = true; @@ -34,7 +44,7 @@ namespace Umbraco.Web.BackOffice.AspNetCore public string ApplicationVirtualPath { get; } public bool IsDebugMode { get; } - + public Version IISVersion { get; } public string LocalTempPath { get @@ -73,24 +83,58 @@ namespace Umbraco.Web.BackOffice.AspNetCore } } - public Version IISVersion { get; } + public string MapPath(string path) => Path.Combine(_webHostEnvironment.WebRootPath, path); - public string ToAbsolute(string virtualPath, string root) => throw new NotImplementedException(); - - public void LazyRestartApplication() + public string ToAbsolute(string virtualPath, string root) { - throw new NotImplementedException(); + if (Uri.TryCreate(virtualPath, UriKind.Absolute, out _)) + { + return virtualPath; + } + + var segment = new PathString(virtualPath.Substring(1)); + var applicationPath = _httpContextAccessor.HttpContext.Request.PathBase; + + return applicationPath.Add(segment).Value; } public void RegisterObject(IRegisteredObject registeredObject) { - throw new NotImplementedException(); + var wrapped = new RegisteredObjectWrapper(registeredObject); + if (!_registeredObjects.TryAdd(registeredObject, wrapped)) + { + throw new InvalidOperationException("Could not register object"); + } + + var cancellationTokenRegistration = _hostApplicationLifetime.ApplicationStopping.Register(() => wrapped.Stop(true)); + wrapped.CancellationTokenRegistration = cancellationTokenRegistration; } public void UnregisterObject(IRegisteredObject registeredObject) { - throw new NotImplementedException(); + if (_registeredObjects.TryGetValue(registeredObject, out var wrapped)) + { + wrapped.CancellationTokenRegistration.Unregister(); + } + } + + + private class RegisteredObjectWrapper + { + private readonly IRegisteredObject _inner; + + public RegisteredObjectWrapper(IRegisteredObject inner) + { + _inner = inner; + } + + public CancellationTokenRegistration CancellationTokenRegistration { get; set; } + + public void Stop(bool immediate) + { + _inner.Stop(immediate); + } } } diff --git a/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreUmbracoApplicationLifetime.cs b/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreUmbracoApplicationLifetime.cs index 1d713794cc..20cfef352d 100644 --- a/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreUmbracoApplicationLifetime.cs +++ b/src/Umbraco.Web.BackOffice/AspNetCore/AspNetCoreUmbracoApplicationLifetime.cs @@ -1,6 +1,6 @@ using System.Threading; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Hosting; using Umbraco.Net; namespace Umbraco.Web.AspNet @@ -8,12 +8,12 @@ namespace Umbraco.Web.AspNet public class AspNetCoreUmbracoApplicationLifetime : IUmbracoApplicationLifetime { private readonly IHttpContextAccessor _httpContextAccessor; - private readonly IApplicationLifetime _applicationLifetime; + private readonly IHostApplicationLifetime _hostApplicationLifetime; - public AspNetCoreUmbracoApplicationLifetime(IHttpContextAccessor httpContextAccessor, IApplicationLifetime applicationLifetime) + public AspNetCoreUmbracoApplicationLifetime(IHttpContextAccessor httpContextAccessor, IHostApplicationLifetime hostApplicationLifetime) { _httpContextAccessor = httpContextAccessor; - _applicationLifetime = applicationLifetime; + _hostApplicationLifetime = hostApplicationLifetime; } public bool IsRestarting { get; set; } @@ -30,7 +30,7 @@ namespace Umbraco.Web.AspNet } Thread.CurrentPrincipal = null; - _applicationLifetime.StopApplication(); + _hostApplicationLifetime.StopApplication(); } } } diff --git a/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs b/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs index 18ec4d6b03..2d12ad3093 100644 --- a/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs @@ -2,6 +2,7 @@ using System.Configuration; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Umbraco.Composing; using Umbraco.Core; using Umbraco.Core.Cache; @@ -33,13 +34,14 @@ namespace Umbraco.Web.BackOffice.AspNetCore var httpContextAccessor = serviceProvider.GetService(); var webHostEnvironment = serviceProvider.GetService(); + var hostApplicationLifetime = serviceProvider.GetService(); var configFactory = new ConfigsFactory(); var hostingSettings = configFactory.HostingSettings; var coreDebug = configFactory.CoreDebug; - var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment); + var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment, httpContextAccessor, hostApplicationLifetime); var ioHelper = new IOHelper(hostingEnvironment); var configs = configFactory.Create(ioHelper);