diff --git a/src/Umbraco.Web/HybridAccessorBase.cs b/src/Umbraco.Web/HybridAccessorBase.cs index 669b929fd6..ec18b6f3d4 100644 --- a/src/Umbraco.Web/HybridAccessorBase.cs +++ b/src/Umbraco.Web/HybridAccessorBase.cs @@ -4,7 +4,16 @@ using Umbraco.Core; namespace Umbraco.Web { - internal abstract class HybridAccessorBase + /// + /// Provides a base class for hybrid accessors. + /// + /// The type of the accessed object. + /// + /// Hybrid accessors store the accessed object in HttpContext if they can, + /// otherwise they rely on the logical call context, to maintain an ambient + /// object that flows with async. + /// + public abstract class HybridAccessorBase where T : class { // ReSharper disable StaticMemberInGenericType @@ -33,7 +42,7 @@ namespace Umbraco.Web // yes! flows with async! private T NonContextValue { - get { return (T) CallContext.LogicalGetData(ItemKey); } + get => (T) CallContext.LogicalGetData(ItemKey); set { if (value == null) CallContext.FreeNamedDataSlot(ItemKey); @@ -43,8 +52,7 @@ namespace Umbraco.Web protected HybridAccessorBase(IHttpContextAccessor httpContextAccessor) { - if (httpContextAccessor == null) throw new ArgumentNullException(nameof(httpContextAccessor)); - _httpContextAccessor = httpContextAccessor; + _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); lock (Locker) { diff --git a/src/Umbraco.Web/HybridUmbracoContextAccessor.cs b/src/Umbraco.Web/HybridUmbracoContextAccessor.cs index e8a07b0f4c..d5d03d58bb 100644 --- a/src/Umbraco.Web/HybridUmbracoContextAccessor.cs +++ b/src/Umbraco.Web/HybridUmbracoContextAccessor.cs @@ -1,17 +1,27 @@ namespace Umbraco.Web { + /// + /// Implements a hybrid . + /// internal class HybridUmbracoContextAccessor : HybridAccessorBase, IUmbracoContextAccessor { + /// + /// Initializes a new instance of the class. + /// public HybridUmbracoContextAccessor(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor) { } + /// protected override string ItemKey => "Umbraco.Web.HybridUmbracoContextAccessor"; + /// + /// Gets or sets the object. + /// public UmbracoContext UmbracoContext { - get { return Value; } - set { Value = value; } + get => Value; + set => Value = value; } } } diff --git a/src/Umbraco.Web/Models/PublishedContent/HybridVariationContextAccessor.cs b/src/Umbraco.Web/Models/PublishedContent/HybridVariationContextAccessor.cs new file mode 100644 index 0000000000..cb002e11b0 --- /dev/null +++ b/src/Umbraco.Web/Models/PublishedContent/HybridVariationContextAccessor.cs @@ -0,0 +1,26 @@ +using Umbraco.Core.Models.PublishedContent; + +namespace Umbraco.Web.Models.PublishedContent +{ + /// + /// Implements a hybrid . + /// + internal class HybridVariationContextAccessor : HybridAccessorBase, IVariationContextAccessor + { + public HybridVariationContextAccessor(IHttpContextAccessor httpContextAccessor) + : base(httpContextAccessor) + { } + + /// + protected override string ItemKey => "Umbraco.Web.HybridVariationContextAccessor"; + + /// + /// Gets or sets the object. + /// + public VariationContext VariationContext + { + get => Value; + set => Value = value; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs b/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs index 037c0e2ea1..79712e01f1 100644 --- a/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs +++ b/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs @@ -69,7 +69,7 @@ namespace Umbraco.Web.Runtime // register accessors for cultures composition.RegisterUnique(); - composition.RegisterUnique(); + composition.RegisterUnique(); // register the http context and umbraco context accessors // we *should* use the HttpContextUmbracoContextAccessor, however there are cases when diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 133da122a1..126e725a50 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -202,6 +202,7 @@ +