2 // System.Web.Hosting.HostingEnvironment.cs
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
10 // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Globalization;
35 using System.Security.Permissions;
36 using System.Threading;
37 using System.Web.Configuration;
38 using System.Web.Caching;
39 using System.Web.Util;
41 namespace System.Web.Hosting {
43 [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
44 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.High)]
45 public sealed class HostingEnvironment : MarshalByRefObject
47 static bool is_hosted;
48 #pragma warning disable 0649
49 static string site_name;
50 static ApplicationShutdownReason shutdown_reason;
51 #pragma warning restore 0649
52 internal static BareApplicationHost Host;
53 static VirtualPathProvider vpath_provider = (HttpRuntime.AppDomainAppVirtualPath == null) ? null :
54 new DefaultVirtualPathProvider ();
55 static int busy_count;
57 internal static bool HaveCustomVPP {
62 public HostingEnvironment ()
64 // The documentation says that this is called once per domain by the ApplicationManager and
65 // then it throws InvalidOperationException whenever called.
66 throw new InvalidOperationException ();
69 public static string ApplicationID {
70 get { return HttpRuntime.AppDomainAppId; }
73 public static string ApplicationPhysicalPath {
74 get { return HttpRuntime.AppDomainAppPath; }
77 public static string ApplicationVirtualPath {
78 get { return HttpRuntime.AppDomainAppVirtualPath; }
81 public static Cache Cache {
82 get { return HttpRuntime.Cache; }
85 public static Exception InitializationException {
86 get { return HttpApplication.InitializationException; }
89 public static bool IsHosted {
90 get { return is_hosted; }
91 internal set { is_hosted = value; }
94 public static ApplicationShutdownReason ShutdownReason {
95 get { return shutdown_reason; }
98 public static string SiteName {
99 get { return site_name; }
100 internal set { site_name = value; }
103 public static VirtualPathProvider VirtualPathProvider {
104 get { return vpath_provider; }
107 public static bool InClientBuildManager {
109 // Mono doesn't have a ClientBuildManager, so we can't be in it. Simple as that.
114 public static void DecrementBusyCount ()
116 Interlocked.Decrement (ref busy_count);
119 [MonoTODO ("Not implemented")]
120 public static IDisposable Impersonate ()
122 throw new NotImplementedException ();
125 [MonoTODO ("Not implemented")]
126 public static IDisposable Impersonate (IntPtr token)
128 throw new NotImplementedException ();
131 [MonoTODO ("Not implemented")]
132 public static IDisposable Impersonate (IntPtr userToken, string virtualPath)
134 throw new NotImplementedException ();
137 public static void IncrementBusyCount ()
139 Interlocked.Increment (ref busy_count);
142 public override object InitializeLifetimeService ()
147 public static void InitiateShutdown ()
149 HttpRuntime.UnloadAppDomain ();
152 public static string MapPath (string virtualPath)
154 if (virtualPath == null || virtualPath == "")
155 throw new ArgumentNullException ("virtualPath");
157 HttpContext context = HttpContext.Current;
158 HttpRequest req = context == null ? null : context.Request;
162 return req.MapPath (virtualPath);
165 public static void RegisterObject (IRegisteredObject obj)
168 throw new ArgumentNullException ("obj");
171 Host.RegisterObject (obj, false);
174 public static void RegisterVirtualPathProvider (VirtualPathProvider virtualPathProvider)
176 if (HttpRuntime.AppDomainAppVirtualPath == null)
177 throw new InvalidOperationException ();
179 if (virtualPathProvider == null)
180 throw new ArgumentNullException ("virtualPathProvider");
182 VirtualPathProvider previous = vpath_provider;
183 vpath_provider = virtualPathProvider;
184 vpath_provider.InitializeAndSetPrevious (previous);
185 if (!(virtualPathProvider is DefaultVirtualPathProvider))
186 HaveCustomVPP = true;
188 HaveCustomVPP = false;
191 public static IDisposable SetCultures (string virtualPath)
193 GlobalizationSection gs = WebConfigurationManager.GetSection ("system.web/globalization", virtualPath) as GlobalizationSection;
194 IDisposable ret = Thread.CurrentThread.CurrentCulture as IDisposable;
195 string culture = gs.Culture;
196 if (String.IsNullOrEmpty (culture))
198 Thread.CurrentThread.CurrentCulture = new CultureInfo (culture);
202 public static IDisposable SetCultures ()
204 return SetCultures ("~/");
207 public static void UnregisterObject (IRegisteredObject obj)
210 throw new ArgumentNullException ("obj");
213 Host.UnregisterObject (obj);