37

I am trying to move into .net core an existing .net application that is using CallContext.LogicalGet/SetData.

When a web request hits the application I save a CorrelationId in the CallContext and whenever I need to log something later down the track I can easily collect it from the CallContext, without the need to transfer it everywhere.

As CallContext is no longer supported in .net core since it is part of System.Messaging.Remoting what options are there?

One version I have seen is that the AsyncLocal could be used (How do the semantics of AsyncLocal differ from the logical call context?) but it looks as if I would have to transmit this variable all over which beats the purpose, it is not as convenient.

Community
  • 1
  • 1
Gradinariu Cezar
  • 553
  • 1
  • 5
  • 12

2 Answers2

29

Had this problem when we switched a library from .Net Framework to .Net Standard and had to replace System.Runtime.Remoting.Messaging CallContext.LogicalGetData and CallContext.LogicalSetData.

I followed this guide to replace the methods:

http://www.cazzulino.com/callcontext-netstandard-netcore.html

/// <summary>
/// Provides a way to set contextual data that flows with the call and 
/// async context of a test or invocation.
/// </summary>
public static class CallContext
{
    static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();

    /// <summary>
    /// Stores a given object and associates it with the specified name.
    /// </summary>
    /// <param name="name">The name with which to associate the new item in the call context.</param>
    /// <param name="data">The object to store in the call context.</param>
    public static void SetData(string name, object data) =>
    state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;

    /// <summary>
    /// Retrieves an object with the specified name from the <see cref="CallContext"/>.
    /// </summary>
    /// <param name="name">The name of the item in the call context.</param>
    /// <returns>The object in the call context associated with the specified name, or <see langword="null"/> if not found.</returns>
    public static object GetData(string name) =>
        state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • 2
    lol, dupes @kzu's original answer 3 months earlier and somehow has more up votes than his. – benmccallum May 07 '21 at 10:16
  • 1
    @benmccallum Yepp, noticed that after posting as well. However posting actual code instead of a link is preferred so I kept the answer. – Ogglas May 07 '21 at 10:25
  • @Ogglas You should have just edited the original answer – Preet Sangha Jul 07 '21 at 00:53
  • @PreetSangha yes but it would have been a complete rewrite then since I wanted to mention `System.Runtime.Remoting.Messaging` `CallContext.LogicalGetData` and `CallContext.LogicalSetData` as well. – Ogglas Jul 07 '21 at 07:38
20

You can use a dictionary of AsyncLocal to simulate exactly the API and behavior of the original CallContext. See http://www.cazzulino.com/callcontext-netstandard-netcore.html for a complete implementation example.

kzu
  • 1,795
  • 15
  • 16