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)