Created
January 24, 2017 10:04
-
-
Save fitzchak/de6af938a4bd3c5fb683bca594734e9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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