11 private static volatile bool _fIsHostedChecked;
13 private static volatile bool _fIsHosted;
19 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
20 private static extern bool HostSecurityManagerPresent();
24 internal static extern int ReleaseHostSecurityContext(
IntPtr context);
28 internal static extern int CloneHostSecurityContext(
SafeHandle context,
SafeHandle clonedContext);
32 private static extern int CaptureHostSecurityContext(
SafeHandle capturedContext);
36 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
37 private static extern int SetHostSecurityContext(
SafeHandle context,
bool fReturnPrevious,
SafeHandle prevContext);
40 internal static bool CheckIfHosted()
42 if (!_fIsHostedChecked)
44 _fIsHosted = HostSecurityManagerPresent();
45 _fIsHostedChecked =
true;
52 [SecuritySafeCritical]
58 IUnknownSafeHandle unknownSafeHandle =
new IUnknownSafeHandle();
60 CaptureHostSecurityContext(unknownSafeHandle);
74 if (hostExecutionContext ==
null)
78 HostExecutionContextSwitcher hostExecutionContextSwitcher =
new HostExecutionContextSwitcher();
80 hostExecutionContextSwitcher.currentHostContext = hostExecutionContext;
81 hostExecutionContextSwitcher.previousHostContext =
null;
82 if (CheckIfHosted() && hostExecutionContext.
State is IUnknownSafeHandle)
84 IUnknownSafeHandle unknownSafeHandle =
new IUnknownSafeHandle();
86 IUnknownSafeHandle context = (IUnknownSafeHandle)hostExecutionContext.
State;
87 SetHostSecurityContext(context, fReturnPrevious:
true, unknownSafeHandle);
89 executionContext.HostExecutionContext = hostExecutionContext;
90 return hostExecutionContextSwitcher;
100 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
101 public virtual void Revert(
object previousState)
103 HostExecutionContextSwitcher hostExecutionContextSwitcher = previousState as HostExecutionContextSwitcher;
104 if (hostExecutionContextSwitcher ==
null)
109 if (mutableExecutionContext != hostExecutionContextSwitcher.executionContext)
113 hostExecutionContextSwitcher.executionContext =
null;
115 if (hostExecutionContext != hostExecutionContextSwitcher.currentHostContext)
120 if (CheckIfHosted() && previousHostContext !=
null && previousHostContext.
State is IUnknownSafeHandle)
122 IUnknownSafeHandle context = (IUnknownSafeHandle)previousHostContext.
State;
123 SetHostSecurityContext(context, fReturnPrevious:
false,
null);
125 mutableExecutionContext.HostExecutionContext = previousHostContext;
134 if (currentHostExecutionContextManager !=
null)
136 result = currentHostExecutionContextManager.
Capture();
142 internal static object SetHostExecutionContextInternal(HostExecutionContext hostContext)
144 HostExecutionContextManager currentHostExecutionContextManager = GetCurrentHostExecutionContextManager();
145 object result =
null;
146 if (currentHostExecutionContextManager !=
null)
148 result = currentHostExecutionContextManager.SetHostExecutionContext(hostContext);
155 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
156 internal static HostExecutionContextManager GetCurrentHostExecutionContextManager()
158 return AppDomainManager.CurrentAppDomainManager?.HostExecutionContextManager;
161 internal static HostExecutionContextManager GetInternalHostExecutionContextManager()
163 if (_hostExecutionContextManager ==
null)
165 _hostExecutionContextManager =
new HostExecutionContextManager();
167 return _hostExecutionContextManager;
static Thread CurrentThread
Gets the currently running thread.
virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
Sets the current host execution context to the specified host execution context.
Encapsulates and propagates the host execution context across threads.
Provides the functionality that allows a common language runtime host to participate in the flow,...
internal object State
Gets or sets the state of the host execution context.
Represents a wrapper class for operating system handles. This class must be inherited.
Cer
Specifies a method's behavior when called within a constrained execution region.
Provides information about, and means to manipulate, the current environment and platform....
Manages the execution context for the current thread. This class cannot be inherited.
A platform-specific type that is used to represent a pointer or a handle.
virtual HostExecutionContext Capture()
Captures the host execution context from the current thread.
MethodImplOptions
Defines the details of how a method is implemented.
virtual void Revert(object previousState)
Restores the host execution context to its prior state.
The exception that is thrown when a method call is invalid for the object's current state.
Consistency
Specifies a reliability contract.
Creates and controls a thread, sets its priority, and gets its status.