From 35f394f2c31b338a8012ce643eb262d3dd9a2225 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Mon, 15 Jul 2019 16:01:13 +0200 Subject: [PATCH] Fallback for context with http context exists. --- src/Squidex.Web/ContextProvider.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Squidex.Web/ContextProvider.cs b/src/Squidex.Web/ContextProvider.cs index 44f1f0a08..ab6f79dbc 100644 --- a/src/Squidex.Web/ContextProvider.cs +++ b/src/Squidex.Web/ContextProvider.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using Microsoft.AspNetCore.Http; using Squidex.Domain.Apps.Entities; using Squidex.Infrastructure; @@ -14,10 +15,24 @@ namespace Squidex.Web public sealed class ContextProvider : IContextProvider { private readonly IHttpContextAccessor httpContextAccessor; + private readonly AsyncLocal asyncLocal = new AsyncLocal(); public Context Context { - get { return httpContextAccessor.HttpContext.Context(); } + get + { + if (httpContextAccessor.HttpContext == null) + { + if (asyncLocal.Value == null) + { + asyncLocal.Value = new Context(); + } + + return asyncLocal.Value; + } + + return httpContextAccessor.HttpContext.Context(); + } } public ContextProvider(IHttpContextAccessor httpContextAccessor)