v9: 10540 Resolving UmbracoHelper in middleware was null

This commit is contained in:
Zeegaan
2021-08-04 12:48:19 +02:00
parent c7dd3b2461
commit 3eb32a97a7
6 changed files with 55 additions and 0 deletions

View File

@@ -161,6 +161,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection
builder.Services.AddScoped<ITagQuery, TagQuery>();
builder.Services.AddUnique<IUmbracoTreeSearcherFields, UmbracoTreeSearcherFields>();
builder.Services.AddSingleton<IPublishedContentQueryAccessor, PublishedContentQueryAccessor>();
builder.Services.AddScoped<IPublishedContentQuery>(factory =>
{
var umbCtx = factory.GetRequiredService<IUmbracoContextAccessor>();

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Cms.Core
{
public interface IPublishedContentQueryAccessor
{
IPublishedContentQuery PublishedContentQuery { get;}
}
}

View File

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

View File

@@ -315,6 +315,7 @@ namespace Umbraco.Extensions
builder.Services.AddSingleton<ContentModelBinder>();
builder.Services.AddSingleton<IUmbracoHelperAccessor, UmbracoHelperAccessor>();
builder.Services.AddScoped<UmbracoHelper>();
builder.Services.AddScoped<IBackOfficeSecurity, BackOfficeSecurity>();

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Cms.Web.Common
{
public interface IUmbracoHelperAccessor
{
UmbracoHelper UmbracoHelper { get;}
}
}

View File

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