Skip to content

Instantly share code, notes, and snippets.

@fitzchak
Created January 24, 2017 10:04
Show Gist options
  • Save fitzchak/de6af938a4bd3c5fb683bca594734e9f to your computer and use it in GitHub Desktop.
Save fitzchak/de6af938a4bd3c5fb683bca594734e9f to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;
namespace AsyncLocalIsDifferentThanCallContext
{
class Program
{
public static AsyncLocal<int> AsyncLocal = new AsyncLocal<int>();
public static int CallContextValue
{
get
{
var data = CallContext.GetData("CallContextValue");
if (data == null)
return 0;
return (int) data;
}
set { CallContext.SetData("CallContextValue", value); }
}
static void Main(string[] args)
{
AsyncLocal.Value = 1;
CallContextValue = 1;
new Thread(() =>
{
Console.WriteLine("From thread AsyncLocal: " + AsyncLocal.Value); // Should be 0 but is 1
Console.WriteLine("From thread CallContext: " + CallContextValue); // Value is 0, as it should be
}).Start();
Console.WriteLine("Main AsyncLocal: " + AsyncLocal.Value);
Console.WriteLine("Main CallContext: " + CallContextValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment