Cleanup hybrid accessors

This commit is contained in:
Stephan
2016-10-20 10:52:11 +02:00
parent acca83a6b1
commit ec68993448
4 changed files with 22 additions and 44 deletions

View File

@@ -7,6 +7,11 @@ namespace Umbraco.Web
internal abstract class HybridAccessorBase<T>
where T : class
{
// ReSharper disable StaticMemberInGenericType
private static readonly object Locker = new object();
private static bool _registered;
// ReSharper restore StaticMemberInGenericType
private readonly IHttpContextAccessor _httpContextAccessor;
protected abstract string ItemKey { get; }
@@ -20,11 +25,6 @@ namespace Umbraco.Web
//
// anything that is ThreadStatic will stay with the thread and NOT flow in async threads
// the only thing that flows is the logical call context (safe in 4.5+)
// now...
// fixme - which tests?!
// tests seem to show that either newing Thread or enqueuing in the ThreadPool both produce a thread
// with a clear logical call context, which would mean that it is somewhat safe to "fire and forget"
// because whatever is in the call context will be gone when the thread returns to the pool
// no!
//[ThreadStatic]
@@ -41,16 +41,20 @@ namespace Umbraco.Web
}
}
// every class inheriting from this class *must* implement a static ctor
// and register itself against the SafeCallContext using this method.
//
// note: because the item key is not static, we cannot register here in the
// base class - unless we do it in the non-static Value property setter,
// with a static bool to keep track of registration - less error-prone but
// perfs impact - considering implementors should be careful.
//
protected static void SafeCallContextRegister(string itemKey)
protected HybridAccessorBase(IHttpContextAccessor httpContextAccessor)
{
if (httpContextAccessor == null) throw new ArgumentNullException(nameof(httpContextAccessor));
_httpContextAccessor = httpContextAccessor;
lock (Locker)
{
// register the itemKey once with SafeCallContext
if (_registered) return;
_registered = true;
}
// ReSharper disable once VirtualMemberCallInConstructor
var itemKey = ItemKey; // virtual
SafeCallContext.Register(() =>
{
var value = CallContext.LogicalGetData(itemKey);
@@ -65,11 +69,6 @@ namespace Umbraco.Web
});
}
protected HybridAccessorBase(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected T Value
{
get

View File

@@ -4,14 +4,7 @@ namespace Umbraco.Web
{
internal class HybridEventMessagesAccessor : HybridAccessorBase<EventMessages>, IEventMessagesAccessor
{
private const string ItemKeyConst = "Umbraco.Core.Events.HybridEventMessagesAccessor";
protected override string ItemKey => ItemKeyConst;
static HybridEventMessagesAccessor()
{
SafeCallContextRegister(ItemKeyConst);
}
protected override string ItemKey => "Umbraco.Core.Events.HybridEventMessagesAccessor";
public HybridEventMessagesAccessor(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)

View File

@@ -2,19 +2,12 @@
{
internal class HybridUmbracoContextAccessor : HybridAccessorBase<UmbracoContext>, IUmbracoContextAccessor
{
private const string ItemKeyConst = "Umbraco.Web.HybridUmbracoContextAccessor";
protected override string ItemKey => ItemKeyConst;
static HybridUmbracoContextAccessor()
{
SafeCallContextRegister(ItemKeyConst);
}
public HybridUmbracoContextAccessor(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{ }
protected override string ItemKey => "Umbraco.Web.HybridUmbracoContextAccessor";
public UmbracoContext UmbracoContext
{
get { return Value; }

View File

@@ -4,14 +4,7 @@ namespace Umbraco.Web
{
internal class HybridUmbracoDatabaseAccessor : HybridAccessorBase<UmbracoDatabase>, IUmbracoDatabaseAccessor
{
private const string ItemKeyConst = "Umbraco.Core.Persistence.HybridUmbracoDatabaseAccessor";
protected override string ItemKey => ItemKeyConst;
static HybridUmbracoDatabaseAccessor()
{
SafeCallContextRegister(ItemKeyConst);
}
protected override string ItemKey => "Umbraco.Core.Persistence.HybridUmbracoDatabaseAccessor";
public HybridUmbracoDatabaseAccessor(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)