HostingEnvironment.cs 5.6 KB

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