More logic in AspNetCoreHostingEnvironment

This commit is contained in:
Bjarke Berg
2020-02-27 11:48:38 +01:00
parent bf2e8c90ed
commit 953c3a667a
3 changed files with 63 additions and 17 deletions

View File

@@ -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<IRegisteredObject, RegisteredObjectWrapper> _registeredObjects =
new ConcurrentDictionary<IRegisteredObject, RegisteredObjectWrapper>();
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);
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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<IHttpContextAccessor>();
var webHostEnvironment = serviceProvider.GetService<IWebHostEnvironment>();
var hostApplicationLifetime = serviceProvider.GetService<IHostApplicationLifetime>();
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);