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 var ec = ExecutionContext.GetCurrentWritable ();
75 ec.LogicalCallContext.FreeNamedDataSlot (name);
76 ec.DataStore [name] = data;
80 public static object LogicalGetData (string name)
82 return LogicalContext.GetData (name);
85 public static void LogicalSetData (string name, object data)
87 var ec = ExecutionContext.GetCurrentWritable ();
88 ec.DataStore.Remove (name);
89 ec.LogicalCallContext.SetData (name, data);
92 public static Header[] GetHeaders ()
97 public static void SetHeaders (Header[] headers)
102 internal static object SetCurrentCallContext (LogicalCallContext ctx)
104 object oldData = new object[] { Datastore, LogicalContext.Datastore };
106 Hashtable logicalDatastore;
107 if (ctx != null && ctx.HasInfo)
108 logicalDatastore = (Hashtable) ctx.Datastore.Clone ();
110 logicalDatastore = null;
112 LogicalContext.Datastore = logicalDatastore;
116 internal static void UpdateCurrentLogicalCallContext (LogicalCallContext ctx)
118 Hashtable data = ctx.Datastore;
122 foreach (DictionaryEntry entry in data)
123 LogicalSetData ((string)entry.Key, entry.Value);
126 internal static void RestoreCallContext (object oldContext)
128 object[] contextArray = (object[])oldContext;
129 ExecutionContext.DataStore = (Dictionary<string, object>)contextArray [0];
130 LogicalContext.Datastore = (Hashtable)contextArray [1];
133 static Dictionary<string, object> Datastore {
135 return ExecutionContext.DataStore;
139 static LogicalCallContext LogicalContext {
141 return ExecutionContext.LogicalCallContext;
145 static ExecutionContext ExecutionContext {
147 return ExecutionContext.Current;
152 [System.Runtime.InteropServices.ComVisible (true)]
153 public interface ILogicalThreadAffinative