HostingEnvironment.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.Globalization;
  32. using System.Security.Permissions;
  33. using System.Threading;
  34. using System.Web.Configuration;
  35. using System.Web.Caching;
  36. using System.Web.Util;
  37. namespace System.Web.Hosting {
  38. [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.High)]
  40. public sealed class HostingEnvironment : MarshalByRefObject
  41. {
  42. static bool is_hosted;
  43. #pragma warning disable 0649
  44. static string site_name;
  45. static ApplicationShutdownReason shutdown_reason;
  46. #pragma warning restore 0649
  47. internal static BareApplicationHost Host;
  48. static VirtualPathProvider vpath_provider = (HttpRuntime.AppDomainAppVirtualPath == null) ? null :
  49. new DefaultVirtualPathProvider ();
  50. static int busy_count;
  51. internal static bool HaveCustomVPP {
  52. get;
  53. private set;
  54. }
  55. public HostingEnvironment ()
  56. {
  57. // The documentation says that this is called once per domain by the ApplicationManager and
  58. // then it throws InvalidOperationException whenever called.
  59. throw new InvalidOperationException ();
  60. }
  61. public static string ApplicationID {
  62. get { return HttpRuntime.AppDomainAppId; }
  63. }
  64. public static string ApplicationPhysicalPath {
  65. get { return HttpRuntime.AppDomainAppPath; }
  66. }
  67. public static string ApplicationVirtualPath {
  68. get { return HttpRuntime.AppDomainAppVirtualPath; }
  69. }
  70. public static Cache Cache {
  71. get { return HttpRuntime.Cache; }
  72. }
  73. public static Exception InitializationException {
  74. get { return HttpApplication.InitializationException; }
  75. }
  76. public static bool IsHosted {
  77. get { return is_hosted; }
  78. internal set { is_hosted = value; }
  79. }
  80. public static ApplicationShutdownReason ShutdownReason {
  81. get { return shutdown_reason; }
  82. }
  83. public static string SiteName {
  84. get { return site_name; }
  85. internal set { site_name = value; }
  86. }
  87. public static VirtualPathProvider VirtualPathProvider {
  88. get { return vpath_provider; }
  89. }
  90. public static bool InClientBuildManager {
  91. get {
  92. // Mono doesn't have a ClientBuildManager, so we can't be in it. Simple as that.
  93. return false;
  94. }
  95. }
  96. public static void DecrementBusyCount ()
  97. {
  98. Interlocked.Decrement (ref busy_count);
  99. }
  100. [MonoTODO ("Not implemented")]
  101. public static IDisposable Impersonate ()
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. [MonoTODO ("Not implemented")]
  106. public static IDisposable Impersonate (IntPtr token)
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. [MonoTODO ("Not implemented")]
  111. public static IDisposable Impersonate (IntPtr userToken, string virtualPath)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. public static void IncrementBusyCount ()
  116. {
  117. Interlocked.Increment (ref busy_count);
  118. }
  119. public override object InitializeLifetimeService ()
  120. {
  121. return null;
  122. }
  123. public static void InitiateShutdown ()
  124. {
  125. HttpRuntime.UnloadAppDomain ();
  126. }
  127. public static string MapPath (string virtualPath)
  128. {
  129. if (virtualPath == null || virtualPath == "")
  130. throw new ArgumentNullException ("virtualPath");
  131. HttpContext context = HttpContext.Current;
  132. HttpRequest req = context == null ? null : context.Request;
  133. if (req == null)
  134. return null;
  135. return req.MapPath (virtualPath);
  136. }
  137. public static void RegisterObject (IRegisteredObject obj)
  138. {
  139. if (obj == null)
  140. throw new ArgumentNullException ("obj");
  141. if (Host != null)
  142. Host.RegisterObject (obj, false);
  143. }
  144. public static void RegisterVirtualPathProvider (VirtualPathProvider virtualPathProvider)
  145. {
  146. if (HttpRuntime.AppDomainAppVirtualPath == null)
  147. throw new InvalidOperationException ();
  148. if (virtualPathProvider == null)
  149. throw new ArgumentNullException ("virtualPathProvider");
  150. VirtualPathProvider previous = vpath_provider;
  151. vpath_provider = virtualPathProvider;
  152. vpath_provider.InitializeAndSetPrevious (previous);
  153. if (!(virtualPathProvider is DefaultVirtualPathProvider))
  154. HaveCustomVPP = true;
  155. else
  156. HaveCustomVPP = false;
  157. }
  158. public static IDisposable SetCultures (string virtualPath)
  159. {
  160. GlobalizationSection gs = WebConfigurationManager.GetSection ("system.web/globalization", virtualPath) as GlobalizationSection;
  161. IDisposable ret = Thread.CurrentThread.CurrentCulture as IDisposable;
  162. string culture = gs.Culture;
  163. if (String.IsNullOrEmpty (culture))
  164. return ret;
  165. Thread.CurrentThread.CurrentCulture = new CultureInfo (culture);
  166. return ret;
  167. }
  168. public static IDisposable SetCultures ()
  169. {
  170. return SetCultures ("~/");
  171. }
  172. public static void UnregisterObject (IRegisteredObject obj)
  173. {
  174. if (obj == null)
  175. throw new ArgumentNullException ("obj");
  176. if (Host != null)
  177. Host.UnregisterObject (obj);
  178. }
  179. }
  180. }