From 3eb32a97a7b02b29fff7dbb7b3b3cd70ca7811f6 Mon Sep 17 00:00:00 2001 From: Zeegaan <70372949+Zeegaan@users.noreply.github.com> Date: Wed, 4 Aug 2021 12:48:19 +0200 Subject: [PATCH] v9: 10540 Resolving UmbracoHelper in middleware was null --- .../UmbracoBuilder.CoreServices.cs | 1 + .../IPublishedContentQueryAccessor.cs | 7 ++++++ .../PublishedContentQueryAccessor.cs | 17 ++++++++++++++ .../UmbracoBuilderExtensions.cs | 1 + .../IUmbracoHelperAccessor.cs | 7 ++++++ .../UmbracoHelperAccessor.cs | 22 +++++++++++++++++++ 6 files changed, 55 insertions(+) create mode 100644 src/Umbraco.Infrastructure/IPublishedContentQueryAccessor.cs create mode 100644 src/Umbraco.Infrastructure/PublishedContentQueryAccessor.cs create mode 100644 src/Umbraco.Web.Common/IUmbracoHelperAccessor.cs create mode 100644 src/Umbraco.Web.Common/UmbracoHelperAccessor.cs diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs index 5b7e4a336c..6ec5128e4a 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs @@ -161,6 +161,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection builder.Services.AddScoped(); builder.Services.AddUnique(); + builder.Services.AddSingleton(); builder.Services.AddScoped(factory => { var umbCtx = factory.GetRequiredService(); diff --git a/src/Umbraco.Infrastructure/IPublishedContentQueryAccessor.cs b/src/Umbraco.Infrastructure/IPublishedContentQueryAccessor.cs new file mode 100644 index 0000000000..f449c40d43 --- /dev/null +++ b/src/Umbraco.Infrastructure/IPublishedContentQueryAccessor.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Cms.Core +{ + public interface IPublishedContentQueryAccessor + { + IPublishedContentQuery PublishedContentQuery { get;} + } +} diff --git a/src/Umbraco.Infrastructure/PublishedContentQueryAccessor.cs b/src/Umbraco.Infrastructure/PublishedContentQueryAccessor.cs new file mode 100644 index 0000000000..4c19292afa --- /dev/null +++ b/src/Umbraco.Infrastructure/PublishedContentQueryAccessor.cs @@ -0,0 +1,17 @@ +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace Umbraco.Cms.Core +{ + public class PublishedContentQueryAccessor : IPublishedContentQueryAccessor + { + private readonly IServiceProvider _serviceProvider; + + public PublishedContentQueryAccessor(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + public IPublishedContentQuery PublishedContentQuery => _serviceProvider.GetRequiredService(); + + } +} diff --git a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs index 957d5976e1..abe2537cf7 100644 --- a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs @@ -315,6 +315,7 @@ namespace Umbraco.Extensions builder.Services.AddSingleton(); + builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/src/Umbraco.Web.Common/IUmbracoHelperAccessor.cs b/src/Umbraco.Web.Common/IUmbracoHelperAccessor.cs new file mode 100644 index 0000000000..9671468631 --- /dev/null +++ b/src/Umbraco.Web.Common/IUmbracoHelperAccessor.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Cms.Web.Common +{ + public interface IUmbracoHelperAccessor + { + UmbracoHelper UmbracoHelper { get;} + } +} diff --git a/src/Umbraco.Web.Common/UmbracoHelperAccessor.cs b/src/Umbraco.Web.Common/UmbracoHelperAccessor.cs new file mode 100644 index 0000000000..765aca792a --- /dev/null +++ b/src/Umbraco.Web.Common/UmbracoHelperAccessor.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace Umbraco.Cms.Web.Common +{ + public class UmbracoHelperAccessor : IUmbracoHelperAccessor + { + private readonly IHttpContextAccessor _httpContextAccessor; + + public UmbracoHelperAccessor(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + public UmbracoHelper UmbracoHelper => _httpContextAccessor.HttpContext.RequestServices.GetRequiredService(); + + } +}