2 // System.Runtime.Remoting.Messaging.CallContext.cs
4 // Authors: Jaime Anguiano Olarra (jaime@gnome.org)
5 // Lluis Sanchez Gual (lluis@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (c) 2002, Jaime Anguiano Olarra
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Threading;
34 using System.Collections;
35 using System.Collections.Generic;
37 namespace System.Runtime.Remoting.Messaging
40 [System.Runtime.InteropServices.ComVisible (true)]
41 public sealed class CallContext
43 [ThreadStatic] static Header [] Headers;
44 [ThreadStatic] static object hostContext;
46 private CallContext ()
50 public static object HostContext {
51 get { return hostContext; }
52 set { hostContext = value; }
55 public static void FreeNamedDataSlot (string name)
57 ExecutionContext.FreeNamedDataSlot (name);
60 public static object GetData (string name)
62 var value = LogicalGetData (name);
64 Datastore.TryGetValue (name, out value);
69 public static void SetData (string name, object data)
71 if (data is ILogicalThreadAffinative) {
72 LogicalSetData (name, data);
74 LogicalContext.FreeNamedDataSlot (name);
75 Datastore [name] = data;
79 public static object LogicalGetData (string name)
81 return LogicalContext.GetData (name);
84 public static void LogicalSetData (string name, object data)
86 Datastore.Remove (name);
87 LogicalContext.SetData (name, data);
90 public static Header[] GetHeaders ()
95 public static void SetHeaders (Header[] headers)
100 internal static object SetCurrentCallContext (LogicalCallContext ctx)
102 object oldData = new object[] { Datastore, LogicalContext.Datastore };
104 Hashtable logicalDatastore;
105 if (ctx != null && ctx.HasInfo)
106 logicalDatastore = (Hashtable) ctx.Datastore.Clone ();
108 logicalDatastore = null;
110 LogicalContext.Datastore = logicalDatastore;
114 internal static void UpdateCurrentLogicalCallContext (LogicalCallContext ctx)
116 Hashtable data = ctx.Datastore;
120 foreach (DictionaryEntry entry in data)
121 LogicalSetData ((string)entry.Key, entry.Value);
124 internal static void RestoreCallContext (object oldContext)
126 object[] contextArray = (object[])oldContext;
127 ExecutionContext.DataStore = (Dictionary<string, object>)contextArray [0];
128 LogicalContext.Datastore = (Hashtable)contextArray [1];
131 static Dictionary<string, object> Datastore {
133 return ExecutionContext.DataStore;
137 static LogicalCallContext LogicalContext {
139 return ExecutionContext.LogicalCallContext;
143 static ExecutionContext ExecutionContext {
145 return ExecutionContext.Current;
150 [System.Runtime.InteropServices.ComVisible (true)]
151 public interface ILogicalThreadAffinative