diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b8fe6a..358474b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Fixes issue where proxied models created from IContent models did not support recursive hydration. * Fixes issue where if a proxy property was overwritten with some value not from CMS, it would not persist and would still use CMS value. +* Fixes issue where preview content breaks due to use of a singleton UmbracoHelper. [#34] ## v1.3.0 diff --git a/ReferenceWebsite/App_Data/Umbraco.sdf b/ReferenceWebsite/App_Data/Umbraco.sdf index 4be8ce1..6af7c7f 100644 Binary files a/ReferenceWebsite/App_Data/Umbraco.sdf and b/ReferenceWebsite/App_Data/Umbraco.sdf differ diff --git a/UmbracoVault/Extensions/DictionaryExtensions.cs b/UmbracoVault/Extensions/DictionaryExtensions.cs new file mode 100644 index 0000000..1adb913 --- /dev/null +++ b/UmbracoVault/Extensions/DictionaryExtensions.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace UmbracoVault.Extensions +{ + internal static class DictionaryExtensions + { + public static object GetOrAddThreadSafe(this IDictionary dictionary, object key, Func valueFactory) + { + if (!dictionary.Contains(key)) + { + lock (key) + { + if (!dictionary.Contains(key)) + { + dictionary.Add(key, valueFactory(key)); + } + } + } + + return dictionary[key]; + } + + public static object GetOrAddThreadSafe(this IDictionary dictionary, object key, object value) + { + return dictionary.GetOrAddThreadSafe(key, k => value); + } + + public static T GetOrAddThreadSafe(this IDictionary dictionary, object key, Func valueFactory) + { + var factoryWrapper = new Func(k => valueFactory(k)); + return (T)dictionary.GetOrAddThreadSafe(key, factoryWrapper); + } + + public static T GetOrAddThreadSafe(this IDictionary dictionary, object key, T value) + { + return dictionary.GetOrAddThreadSafe(key, k => value); + } + } +} \ No newline at end of file diff --git a/UmbracoVault/UmbracoContext.cs b/UmbracoVault/UmbracoContext.cs index 12f6fee..2675939 100644 --- a/UmbracoVault/UmbracoContext.cs +++ b/UmbracoVault/UmbracoContext.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Web; using Umbraco.Core; using Umbraco.Core.Logging; @@ -28,8 +29,7 @@ public class UmbracoWebContext : BaseUmbracoContext //new SuperScriptTransformation() }; - private UmbracoHelper _helper; - protected UmbracoHelper Helper => _helper ?? (_helper = new UmbracoHelper(UmbracoContext.Current)); + protected UmbracoHelper Helper => GetUmbracoHelperForRequest(); private IPublishedContent GetCurrentUmbracoContent() { @@ -166,6 +166,12 @@ protected T GetItem(IPublishedContent n) return result; } + private static UmbracoHelper GetUmbracoHelperForRequest() + { + const string umbracoHelperKey = "__vaultUmbracoHelper"; + return HttpContext.Current?.Items.GetOrAddThreadSafe(string.Intern(umbracoHelperKey), new UmbracoHelper(UmbracoContext.Current)); + } + public static ReadOnlyCollection GetUmbracoEntityAliasesFromType(Type type) { var results = new HashSet(); diff --git a/UmbracoVault/UmbracoVault.csproj b/UmbracoVault/UmbracoVault.csproj index 2597ee8..d8f61a8 100644 --- a/UmbracoVault/UmbracoVault.csproj +++ b/UmbracoVault/UmbracoVault.csproj @@ -87,6 +87,7 @@ + diff --git a/UmbracoVault/UmbracoVault.nuspec b/UmbracoVault/UmbracoVault.nuspec index 2401fc5..32f864f 100644 --- a/UmbracoVault/UmbracoVault.nuspec +++ b/UmbracoVault/UmbracoVault.nuspec @@ -14,6 +14,7 @@ * Fixes issue where proxied models created from IContent models did not support recursive hydration. * Fixes issue where if a proxy property was overwritten with some value not from CMS, it would not persist and would still use CMS value. + * Fixes issue where preview content breaks due to use of a singleton UmbracoHelper. [#34] (c) The Nerdery LLC 2016. All Rights Reserved. Umbraco UmbracoVault Mapping ObjectMapper ORM CMS