{
[Serializable]
+ [System.Runtime.InteropServices.ComVisible (true)]
public sealed class CallContext
{
- internal const string HeadersKey = "__Headers";
- internal const string ContextKey = "__CallContext";
+ [ThreadStatic] static Header [] Headers;
+ [ThreadStatic] static Hashtable logicalDatastore;
+ [ThreadStatic] static Hashtable datastore;
+ [ThreadStatic] static object hostContext;
private CallContext ()
{
}
+ public static object HostContext {
+ get { return hostContext; }
+ set { hostContext = value; }
+ }
+
// public methods
public static void FreeNamedDataSlot (string name)
{
Datastore.Remove (name);
+ LogicalDatastore.Remove (name);
}
public static object GetData (string name)
{
- return Datastore [name];
+ if (LogicalDatastore.ContainsKey (name)) {
+ return LogicalDatastore [name];
+ } else {
+ return Datastore [name];
+ }
}
-
- public static Header[] GetHeaders ()
+
+ public static void SetData (string name, object data)
+ {
+ if (data is ILogicalThreadAffinative) {
+ LogicalSetData (name, data);
+ } else {
+ LogicalDatastore.Remove (name);
+ Datastore [name] = data;
+ }
+ }
+
+ public static object LogicalGetData (string name)
{
- return (Header[]) Datastore [HeadersKey];
+ return LogicalDatastore [name];
}
- public static void SetData (string name, object data)
+ public static void LogicalSetData (string name, object data)
{
- Datastore [name] = data;
+ Datastore.Remove (name);
+ LogicalDatastore [name] = data;
}
+ public static Header[] GetHeaders ()
+ {
+ return Headers;
+ }
+
public static void SetHeaders (Header[] headers)
{
- Datastore [HeadersKey] = headers;
+ Headers = headers;
}
- internal static LogicalCallContext CreateLogicalCallContext ()
+ internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
{
- LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
- Hashtable res = (Hashtable) Thread.GetData (ds);
-
- LogicalCallContext ctx = new LogicalCallContext();
- if (res == null) return ctx;
-
- foreach (DictionaryEntry entry in res)
- if (entry.Value is ILogicalThreadAffinative)
+ LogicalCallContext ctx = null;
+ if (logicalDatastore != null) {
+ ctx = new LogicalCallContext ();
+ foreach (DictionaryEntry entry in logicalDatastore) {
ctx.SetData ((string)entry.Key, entry.Value);
+ }
+ }
- return ctx;
+ if (ctx == null && createEmpty)
+ return new LogicalCallContext ();
+ else
+ return ctx;
}
internal static object SetCurrentCallContext (LogicalCallContext ctx)
{
- LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
- object oldData = Thread.GetData (ds);
+ object oldData = new object[] { datastore, logicalDatastore };
if (ctx != null && ctx.HasInfo)
- {
- Hashtable newData = new Hashtable();
- Hashtable data = ctx.Datastore;
-
- foreach (DictionaryEntry entry in data)
- newData [(string)entry.Key] = entry.Value;
-
- Thread.SetData (ds, newData);
- }
+ logicalDatastore = (Hashtable) ctx.Datastore.Clone ();
else
- Thread.SetData (ds, null);
+ logicalDatastore = null;
return oldData;
}
- internal static void UpdateCurrentCallContext (LogicalCallContext ctx)
+ internal static void UpdateCurrentLogicalCallContext (LogicalCallContext ctx)
{
Hashtable data = ctx.Datastore;
+ if (data == null)
+ return;
+
foreach (DictionaryEntry entry in data)
- SetData ((string)entry.Key, entry.Value);
+ LogicalSetData ((string)entry.Key, entry.Value);
}
internal static void RestoreCallContext (object oldContext)
{
- LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
- Thread.SetData (ds, oldContext);
+ object[] contextArray = (object[])oldContext;
+ datastore = (Hashtable)contextArray [0];
+ logicalDatastore = (Hashtable)contextArray [1];
}
private static Hashtable Datastore
{
- get
- {
- LocalDataStoreSlot ds = Thread.GetNamedDataSlot (ContextKey);
- Hashtable res = (Hashtable) Thread.GetData (ds);
- if (res == null) {
- res = new Hashtable ();
- Thread.SetData (ds, res);
- }
- return res;
+ get {
+ Hashtable r = datastore;
+ if (r == null)
+ return datastore = new Hashtable ();
+ return r;
+ }
+ }
+
+ private static Hashtable LogicalDatastore
+ {
+ get {
+ Hashtable r = logicalDatastore;
+ if (r == null)
+ return logicalDatastore = new Hashtable ();
+ return r;
}
}
}
+ [System.Runtime.InteropServices.ComVisible (true)]
public interface ILogicalThreadAffinative
{
}