mscorlib(4.0.0.0) API with additions
LogicalCallContext.cs
1 using System.Collections;
4 using System.Security;
6 
8 {
10  [Serializable]
11  [SecurityCritical]
12  [ComVisible(true)]
14  {
15  internal struct Reader
16  {
17  private LogicalCallContext m_ctx;
18 
19  public bool IsNull => m_ctx == null;
20 
21  public bool HasInfo
22  {
23  get
24  {
25  if (!IsNull)
26  {
27  return m_ctx.HasInfo;
28  }
29  return false;
30  }
31  }
32 
33  public IPrincipal Principal
34  {
35  get
36  {
37  if (!IsNull)
38  {
39  return m_ctx.Principal;
40  }
41  return null;
42  }
43  }
44 
45  public object HostContext
46  {
47  get
48  {
49  if (!IsNull)
50  {
51  return m_ctx.HostContext;
52  }
53  return null;
54  }
55  }
56 
57  public Reader(LogicalCallContext ctx)
58  {
59  m_ctx = ctx;
60  }
61 
62  public LogicalCallContext Clone()
63  {
64  return (LogicalCallContext)m_ctx.Clone();
65  }
66 
67  [SecurityCritical]
68  public object GetData(string name)
69  {
70  if (!IsNull)
71  {
72  return m_ctx.GetData(name);
73  }
74  return null;
75  }
76  }
77 
78  private static Type s_callContextType = typeof(LogicalCallContext);
79 
80  private const string s_CorrelationMgrSlotName = "System.Diagnostics.Trace.CorrelationManagerSlot";
81 
82  private Hashtable m_Datastore;
83 
84  private CallContextRemotingData m_RemotingData;
85 
86  private CallContextSecurityData m_SecurityData;
87 
88  private object m_HostContext;
89 
90  private bool m_IsCorrelationMgr;
91 
92  private Header[] _sendHeaders;
93 
94  private Header[] _recvHeaders;
95 
98  public bool HasInfo
99  {
100  [SecurityCritical]
101  get
102  {
103  bool result = false;
104  if ((m_RemotingData != null && m_RemotingData.HasInfo) || (m_SecurityData != null && m_SecurityData.HasInfo) || m_HostContext != null || HasUserData)
105  {
106  result = true;
107  }
108  return result;
109  }
110  }
111 
112  private bool HasUserData
113  {
114  get
115  {
116  if (m_Datastore != null)
117  {
118  return m_Datastore.Count > 0;
119  }
120  return false;
121  }
122  }
123 
124  internal CallContextRemotingData RemotingData
125  {
126  get
127  {
128  if (m_RemotingData == null)
129  {
130  m_RemotingData = new CallContextRemotingData();
131  }
132  return m_RemotingData;
133  }
134  }
135 
136  internal CallContextSecurityData SecurityData
137  {
138  get
139  {
140  if (m_SecurityData == null)
141  {
142  m_SecurityData = new CallContextSecurityData();
143  }
144  return m_SecurityData;
145  }
146  }
147 
148  internal object HostContext
149  {
150  get
151  {
152  return m_HostContext;
153  }
154  set
155  {
156  m_HostContext = value;
157  }
158  }
159 
160  private Hashtable Datastore
161  {
162  get
163  {
164  if (m_Datastore == null)
165  {
166  m_Datastore = new Hashtable();
167  }
168  return m_Datastore;
169  }
170  }
171 
172  internal IPrincipal Principal
173  {
174  get
175  {
176  if (m_SecurityData != null)
177  {
178  return m_SecurityData.Principal;
179  }
180  return null;
181  }
182  [SecurityCritical]
183  set
184  {
185  SecurityData.Principal = value;
186  }
187  }
188 
189  internal LogicalCallContext()
190  {
191  }
192 
193  [SecurityCritical]
194  internal LogicalCallContext(SerializationInfo info, StreamingContext context)
195  {
196  SerializationInfoEnumerator enumerator = info.GetEnumerator();
197  while (enumerator.MoveNext())
198  {
199  if (enumerator.Name.Equals("__RemotingData"))
200  {
201  m_RemotingData = (CallContextRemotingData)enumerator.Value;
202  }
203  else if (enumerator.Name.Equals("__SecurityData"))
204  {
205  if (context.State == StreamingContextStates.CrossAppDomain)
206  {
207  m_SecurityData = (CallContextSecurityData)enumerator.Value;
208  }
209  }
210  else if (enumerator.Name.Equals("__HostContext"))
211  {
212  m_HostContext = enumerator.Value;
213  }
214  else if (enumerator.Name.Equals("__CorrelationMgrSlotPresent"))
215  {
216  m_IsCorrelationMgr = (bool)enumerator.Value;
217  }
218  else
219  {
220  Datastore[enumerator.Name] = enumerator.Value;
221  }
222  }
223  }
224 
231  [SecurityCritical]
233  {
234  if (info == null)
235  {
236  throw new ArgumentNullException("info");
237  }
238  info.SetType(s_callContextType);
239  if (m_RemotingData != null)
240  {
241  info.AddValue("__RemotingData", m_RemotingData);
242  }
243  if (m_SecurityData != null && context.State == StreamingContextStates.CrossAppDomain)
244  {
245  info.AddValue("__SecurityData", m_SecurityData);
246  }
247  if (m_HostContext != null)
248  {
249  info.AddValue("__HostContext", m_HostContext);
250  }
251  if (m_IsCorrelationMgr)
252  {
253  info.AddValue("__CorrelationMgrSlotPresent", m_IsCorrelationMgr);
254  }
255  if (HasUserData)
256  {
257  IDictionaryEnumerator enumerator = m_Datastore.GetEnumerator();
258  while (enumerator.MoveNext())
259  {
260  info.AddValue((string)enumerator.Key, enumerator.Value);
261  }
262  }
263  }
264 
267  [SecuritySafeCritical]
268  public object Clone()
269  {
270  LogicalCallContext logicalCallContext = new LogicalCallContext();
271  if (m_RemotingData != null)
272  {
273  logicalCallContext.m_RemotingData = (CallContextRemotingData)m_RemotingData.Clone();
274  }
275  if (m_SecurityData != null)
276  {
277  logicalCallContext.m_SecurityData = (CallContextSecurityData)m_SecurityData.Clone();
278  }
279  if (m_HostContext != null)
280  {
281  logicalCallContext.m_HostContext = m_HostContext;
282  }
283  logicalCallContext.m_IsCorrelationMgr = m_IsCorrelationMgr;
284  if (HasUserData)
285  {
286  IDictionaryEnumerator enumerator = m_Datastore.GetEnumerator();
287  if (!m_IsCorrelationMgr)
288  {
289  while (enumerator.MoveNext())
290  {
291  logicalCallContext.Datastore[(string)enumerator.Key] = enumerator.Value;
292  }
293  }
294  else
295  {
296  while (enumerator.MoveNext())
297  {
298  string text = (string)enumerator.Key;
299  if (text.Equals("System.Diagnostics.Trace.CorrelationManagerSlot"))
300  {
301  logicalCallContext.Datastore[text] = ((ICloneable)enumerator.Value).Clone();
302  }
303  else
304  {
305  logicalCallContext.Datastore[text] = enumerator.Value;
306  }
307  }
308  }
309  }
310  return logicalCallContext;
311  }
312 
313  [SecurityCritical]
314  internal void Merge(LogicalCallContext lc)
315  {
316  if (lc != null && this != lc && lc.HasUserData)
317  {
318  IDictionaryEnumerator enumerator = lc.Datastore.GetEnumerator();
319  while (enumerator.MoveNext())
320  {
321  Datastore[(string)enumerator.Key] = enumerator.Value;
322  }
323  }
324  }
325 
328  [SecurityCritical]
329  public void FreeNamedDataSlot(string name)
330  {
331  Datastore.Remove(name);
332  }
333 
337  [SecurityCritical]
338  public object GetData(string name)
339  {
340  return Datastore[name];
341  }
342 
346  [SecurityCritical]
347  public void SetData(string name, object data)
348  {
349  Datastore[name] = data;
350  if (name.Equals("System.Diagnostics.Trace.CorrelationManagerSlot"))
351  {
352  m_IsCorrelationMgr = true;
353  }
354  }
355 
356  private Header[] InternalGetOutgoingHeaders()
357  {
358  Header[] sendHeaders = _sendHeaders;
359  _sendHeaders = null;
360  _recvHeaders = null;
361  return sendHeaders;
362  }
363 
364  internal void InternalSetHeaders(Header[] headers)
365  {
366  _sendHeaders = headers;
367  _recvHeaders = null;
368  }
369 
370  internal Header[] InternalGetHeaders()
371  {
372  if (_sendHeaders != null)
373  {
374  return _sendHeaders;
375  }
376  return _recvHeaders;
377  }
378 
379  [SecurityCritical]
380  internal IPrincipal RemovePrincipalIfNotSerializable()
381  {
382  IPrincipal principal = Principal;
383  if (principal != null && !principal.GetType().IsSerializable)
384  {
385  Principal = null;
386  }
387  return principal;
388  }
389 
390  [SecurityCritical]
391  internal void PropagateOutgoingHeadersToMessage(IMessage msg)
392  {
393  Header[] array = InternalGetOutgoingHeaders();
394  if (array == null)
395  {
396  return;
397  }
398  IDictionary properties = msg.Properties;
399  Header[] array2 = array;
400  foreach (Header header in array2)
401  {
402  if (header != null)
403  {
404  string propertyKeyForHeader = GetPropertyKeyForHeader(header);
405  properties[propertyKeyForHeader] = header;
406  }
407  }
408  }
409 
410  internal static string GetPropertyKeyForHeader(Header header)
411  {
412  if (header == null)
413  {
414  return null;
415  }
416  if (header.HeaderNamespace != null)
417  {
418  return header.Name + ", " + header.HeaderNamespace;
419  }
420  return header.Name;
421  }
422 
423  [SecurityCritical]
424  internal void PropagateIncomingHeadersToCallContext(IMessage msg)
425  {
426  IInternalMessage internalMessage = msg as IInternalMessage;
427  if (internalMessage != null && !internalMessage.HasProperties())
428  {
429  return;
430  }
431  IDictionary properties = msg.Properties;
432  IDictionaryEnumerator enumerator = properties.GetEnumerator();
433  int num = 0;
434  while (enumerator.MoveNext())
435  {
436  string text = (string)enumerator.Key;
437  if (!text.StartsWith("__", StringComparison.Ordinal) && enumerator.Value is Header)
438  {
439  num++;
440  }
441  }
442  Header[] array = null;
443  if (num > 0)
444  {
445  array = new Header[num];
446  num = 0;
447  enumerator.Reset();
448  while (enumerator.MoveNext())
449  {
450  string text2 = (string)enumerator.Key;
451  if (!text2.StartsWith("__", StringComparison.Ordinal))
452  {
453  Header header = enumerator.Value as Header;
454  if (header != null)
455  {
456  array[num++] = header;
457  }
458  }
459  }
460  }
461  _recvHeaders = array;
462  _sendHeaders = null;
463  }
464  }
465 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
object Clone()
Creates a new object that is a copy of the current instance.
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
bool MoveNext()
Advances the enumerator to the next element of the collection.
StreamingContextStates State
Gets the source or destination of the transmitted data.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
void SetData(string name, object data)
Stores the specified object in the current instance, and associates it with the specified name.
object Value
Gets the value of the item currently being examined.
bool HasInfo
Gets a value indicating whether the current T:System.Runtime.Remoting.Messaging.LogicalCallContext co...
Defines the basic functionality of a principal object.
Definition: IPrincipal.cs:8
object Key
Gets the key of the current dictionary entry.
Definition: __Canon.cs:3
virtual object Clone()
Creates a shallow copy of the T:System.Collections.Hashtable.
Definition: Hashtable.cs:946
object GetData(string name)
Retrieves an object associated with the specified name from the current instance.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a specified T:System.Runtime.Serialization.SerializationInfo with the data needed to serial...
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
Defines the out-of-band data for a call.
Definition: Header.cs:8
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Provides a set of properties that are carried with the execution code path during remote method calls...
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
new IDictionaryEnumerator GetEnumerator()
Returns an T:System.Collections.IDictionaryEnumerator object for the T:System.Collections....
object Value
Gets the value of the current dictionary entry.
Specifies that the class can be serialized.
Enumerates the elements of a nongeneric dictionary.
string Name
Gets the name for the item currently being examined.
StreamingContextStates
Defines a set of flags that specifies the source or destination context for the stream during seriali...
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
Represents a nongeneric collection of key/value pairs.
Definition: IDictionary.cs:8
void FreeNamedDataSlot(string name)
Empties a data slot with the specified name.
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658
bool MoveNext()
Updates the enumerator to the next item.