HostingEnvironment.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // System.Web.Hosting.HostingEnvironment.cs
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Globalization;
  33. using System.Linq;
  34. using System.Security.Permissions;
  35. using System.Threading;
  36. using System.Threading.Tasks;
  37. using System.Web.Configuration;
  38. using System.Web.Caching;
  39. using System.Web.Util;
  40. namespace System.Web.Hosting {
  41. [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
  42. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.High)]
  43. public sealed class HostingEnvironment : MarshalByRefObject
  44. {
  45. static bool is_hosted;
  46. #pragma warning disable 0649
  47. static string site_name;
  48. static ApplicationShutdownReason shutdown_reason;
  49. #pragma warning restore 0649
  50. internal static BareApplicationHost Host;
  51. static VirtualPathProvider vpath_provider = (HttpRuntime.AppDomainAppVirtualPath == null) ? null :
  52. new DefaultVirtualPathProvider ();
  53. static int busy_count;
  54. static BackgroundWorkScheduler _backgroundWorkScheduler = null; // created on demand
  55. static readonly Task<object> _completedTask = Task.FromResult<object>(null);
  56. internal static bool HaveCustomVPP {
  57. get;
  58. private set;
  59. }
  60. public HostingEnvironment ()
  61. {
  62. // The documentation says that this is called once per domain by the ApplicationManager and
  63. // then it throws InvalidOperationException whenever called.
  64. throw new InvalidOperationException ();
  65. }
  66. public static string ApplicationID {
  67. get { return HttpRuntime.AppDomainAppId; }
  68. }
  69. public static string ApplicationPhysicalPath {
  70. get { return HttpRuntime.AppDomainAppPath; }
  71. }
  72. public static string ApplicationVirtualPath {
  73. get { return HttpRuntime.AppDomainAppVirtualPath; }
  74. }
  75. public static Cache Cache {
  76. get { return HttpRuntime.Cache; }
  77. }
  78. public static Exception InitializationException {
  79. get { return HttpApplication.InitializationException; }
  80. }
  81. public static bool IsHosted {
  82. get { return is_hosted; }
  83. internal set { is_hosted = value; }
  84. }
  85. public static ApplicationShutdownReason ShutdownReason {
  86. get { return shutdown_reason; }
  87. }
  88. public static string SiteName {
  89. get { return site_name; }
  90. internal set { site_name = value; }
  91. }
  92. public static VirtualPathProvider VirtualPathProvider {
  93. get { return vpath_provider; }
  94. }
  95. public static bool InClientBuildManager {
  96. get {
  97. // Mono doesn't have a ClientBuildManager, so we can't be in it. Simple as that.
  98. return false;
  99. }
  100. }
  101. public static void DecrementBusyCount ()
  102. {
  103. Interlocked.Decrement (ref busy_count);
  104. }
  105. [MonoTODO ("Not implemented")]
  106. public static IDisposable Impersonate ()
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. [MonoTODO ("Not implemented")]
  111. public static IDisposable Impersonate (IntPtr token)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. [MonoTODO ("Not implemented")]
  116. public static IDisposable Impersonate (IntPtr userToken, string virtualPath)
  117. {
  118. throw new NotImplementedException ();
  119. }
  120. public static void IncrementBusyCount ()
  121. {
  122. Interlocked.Increment (ref busy_count);
  123. }
  124. public override object InitializeLifetimeService ()
  125. {
  126. return null;
  127. }
  128. public static void InitiateShutdown ()
  129. {
  130. HttpRuntime.UnloadAppDomain ();
  131. }
  132. public static string MapPath (string virtualPath)
  133. {
  134. if (virtualPath == null || virtualPath == "")
  135. throw new ArgumentNullException ("virtualPath");
  136. HttpContext context = HttpContext.Current;
  137. HttpRequest req = context == null ? null : context.Request;
  138. if (req == null)
  139. return null;
  140. return req.MapPath (virtualPath);
  141. }
  142. public static void RegisterObject (IRegisteredObject obj)
  143. {
  144. if (obj == null)
  145. throw new ArgumentNullException ("obj");
  146. if (Host != null)
  147. Host.RegisterObject (obj, false);
  148. }
  149. public static void RegisterVirtualPathProvider (VirtualPathProvider virtualPathProvider)
  150. {
  151. if (HttpRuntime.AppDomainAppVirtualPath == null)
  152. throw new InvalidOperationException ();
  153. if (virtualPathProvider == null)
  154. throw new ArgumentNullException ("virtualPathProvider");
  155. VirtualPathProvider previous = vpath_provider;
  156. vpath_provider = virtualPathProvider;
  157. vpath_provider.InitializeAndSetPrevious (previous);
  158. if (!(virtualPathProvider is DefaultVirtualPathProvider))
  159. HaveCustomVPP = true;
  160. else
  161. HaveCustomVPP = false;
  162. }
  163. public static IDisposable SetCultures (string virtualPath)
  164. {
  165. GlobalizationSection gs = WebConfigurationManager.GetSection ("system.web/globalization", virtualPath) as GlobalizationSection;
  166. IDisposable ret = Thread.CurrentThread.CurrentCulture as IDisposable;
  167. string culture = gs.Culture;
  168. if (String.IsNullOrEmpty (culture))
  169. return ret;
  170. Thread.CurrentThread.CurrentCulture = new CultureInfo (culture);
  171. return ret;
  172. }
  173. public static IDisposable SetCultures ()
  174. {
  175. return SetCultures ("~/");
  176. }
  177. public static void UnregisterObject (IRegisteredObject obj)
  178. {
  179. if (obj == null)
  180. throw new ArgumentNullException ("obj");
  181. if (Host != null)
  182. Host.UnregisterObject (obj);
  183. }
  184. // Schedules a task which can run in the background, independent of any request.
  185. // This differs from a normal ThreadPool work item in that ASP.NET can keep track
  186. // of how many work items registered through this API are currently running, and
  187. // the ASP.NET runtime will try not to delay AppDomain shutdown until these work
  188. // items have finished executing.
  189. //
  190. // Usage notes:
  191. // - This API cannot be called outside of an ASP.NET-managed AppDomain.
  192. // - The caller's ExecutionContext is not flowed to the work item.
  193. // - Scheduled work items are not guaranteed to ever execute, e.g., when AppDomain
  194. // shutdown has already started by the time this API was called.
  195. // - The provided CancellationToken will be signaled when the application is
  196. // shutting down. The work item should make every effort to honor this token.
  197. // If a work item does not honor this token and continues executing it will
  198. // eventually be considered rogue, and the ASP.NET runtime will rudely unload
  199. // the AppDomain without waiting for the work item to finish.
  200. //
  201. // This overload of QueueBackgroundWorkItem takes a void-returning callback; the
  202. // work item will be considered finished when the callback returns.
  203. [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
  204. public static void QueueBackgroundWorkItem(Action<CancellationToken> workItem) {
  205. if (workItem == null) {
  206. throw new ArgumentNullException("workItem");
  207. }
  208. QueueBackgroundWorkItem(ct => { workItem(ct); return _completedTask; });
  209. }
  210. // See documentation on the other overload for a general API overview.
  211. //
  212. // This overload of QueueBackgroundWorkItem takes a Task-returning callback; the
  213. // work item will be considered finished when the returned Task transitions to a
  214. // terminal state.
  215. [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
  216. public static void QueueBackgroundWorkItem(Func<CancellationToken, Task> workItem) {
  217. if (workItem == null) {
  218. throw new ArgumentNullException("workItem");
  219. }
  220. if (Host == null) {
  221. throw new InvalidOperationException(); // can only be called within an ASP.NET AppDomain
  222. }
  223. QueueBackgroundWorkItemInternal(workItem);
  224. }
  225. static void QueueBackgroundWorkItemInternal(Func<CancellationToken, Task> workItem) {
  226. Debug.Assert(workItem != null);
  227. BackgroundWorkScheduler scheduler = Volatile.Read(ref _backgroundWorkScheduler);
  228. // If the scheduler doesn't exist, lazily create it, but only allow one instance to ever be published to the backing field
  229. if (scheduler == null) {
  230. BackgroundWorkScheduler newlyCreatedScheduler = new BackgroundWorkScheduler(UnregisterObject, WriteUnhandledException);
  231. scheduler = Interlocked.CompareExchange(ref _backgroundWorkScheduler, newlyCreatedScheduler, null) ?? newlyCreatedScheduler;
  232. if (scheduler == newlyCreatedScheduler) {
  233. RegisterObject(scheduler); // Only call RegisterObject if we just created the "winning" one
  234. }
  235. }
  236. scheduler.ScheduleWorkItem(workItem);
  237. }
  238. static void WriteUnhandledException (AppDomain appDomain, Exception exception)
  239. {
  240. Console.Error.WriteLine ("Error in background work item: " + exception);
  241. }
  242. }
  243. }