Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AsyncLocal issue by following exactly HttpContextAccessor usage. #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions TestAsyncLocalMiddleware/Infrastructure/ContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ IDictionary<string, IEnumerable<string>> headers
{

var contextCollection = new Dictionary<string, IEnumerable<string>>();
foreach (var kv in securityTokenAccessor.ContextItems)
if (securityTokenAccessor.ContextItems != null)
{
contextCollection.Add(kv.Key, new[] { kv.Value.ToString() });
foreach (var kv in securityTokenAccessor.ContextItems)
{
contextCollection.Add(kv.Key, new[] { kv.Value.ToString() });
}
}

if (headers != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface ISecurityTokenAccessor
IDictionary<string, object> ContextItems
{
get;
set;
}
}
}
35 changes: 21 additions & 14 deletions TestAsyncLocalMiddleware/Infrastructure/SecurityTokenAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,38 @@ namespace TestAsyncLocalMiddleware.Infrastructure
{
public class SecurityTokenAccessor : ISecurityTokenAccessor
{
private readonly AsyncLocal<IDictionary<string, object>> _asyncLocalDictionary = new AsyncLocal<IDictionary<string, object>>();
private readonly AsyncLocal<ContextHolder> _asyncLocalDictionary = new AsyncLocal<ContextHolder>();

public IDictionary<string, object> ContextItems
{
get
{
var result = GetThreadLocal();
if (result == null)
return _asyncLocalDictionary.Value?.Context;
}
set
{
var holder = _asyncLocalDictionary.Value;
if (holder != null)
{
// Clear current HttpContext trapped in the AsyncLocals, as its done.
holder.Context = null;
}

if (value != null)
{
result = new Dictionary<string, object>();
SetThreadLocal(result);
// Use an object indirection to hold the dictionary in the AsyncLocal,
// so it can be cleared in all ExecutionContexts when its cleared.
_asyncLocalDictionary.Value = new ContextHolder { Context = value };
}
return result;
}
}

private IDictionary<string, object> GetThreadLocal()
{
return _asyncLocalDictionary.Value;
}


private void SetThreadLocal(IDictionary<string, object> contextItems)
{
_asyncLocalDictionary.Value = contextItems;
}
}

public class ContextHolder
{
public IDictionary<string, Object> Context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ public async Task Invoke(HttpContext context, ILog log)
/// </summary>
private void PopulateTokenAccessor(IHeaderDictionary headers)
{
var contextItems = new Dictionary<string, object>();
//CorpId
if (headers.ContainsKey(AuthCorpIdHeader))
_securityTokenAccessor.ContextItems.Add(ContextBuilder.CorpIdHeader, headers[AuthCorpIdHeader].First());
contextItems.Add(ContextBuilder.CorpIdHeader, headers[AuthCorpIdHeader].First());

// UserId
if (headers.ContainsKey(AuthUserHeader))
_securityTokenAccessor.ContextItems.Add(ContextBuilder.UserIdHeader, headers[AuthUserHeader].First());
contextItems.Add(ContextBuilder.UserIdHeader, headers[AuthUserHeader].First());

_securityTokenAccessor.ContextItems = contextItems;

}
}
Expand Down