2 // System.Web.Hosting.ApplicationHost.cs
5 // Miguel de Icaza (miguel@novell.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Configuration;
32 using System.Security.Permissions;
33 using System.Security.Policy;
35 using System.Web.Configuration;
37 namespace System.Web.Hosting {
39 // CAS - no InheritanceDemand here as the class is sealed
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public sealed class ApplicationHost {
42 const string DEFAULT_WEB_CONFIG_NAME = "web.config";
43 internal const string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
45 static object create_dir = new object ();
51 internal static string FindWebConfig (string basedir)
53 if (String.IsNullOrEmpty (basedir) || !Directory.Exists (basedir))
56 string[] files = Directory.GetFileSystemEntries (basedir, "?eb.?onfig");
57 if (files == null || files.Length == 0)
62 internal static bool ClearDynamicBaseDirectory (string directory)
64 string[] entries = null;
67 entries = Directory.GetDirectories (directory);
73 if (entries != null && entries.Length > 0) {
74 foreach (string e in entries) {
75 if (ClearDynamicBaseDirectory (e)) {
86 entries = Directory.GetFiles (directory);
91 if (entries != null && entries.Length > 0) {
92 foreach (string e in entries) {
104 static bool CreateDirectory (string directory)
107 if (!Directory.Exists (directory)) {
108 Directory.CreateDirectory (directory);
115 static string BuildPrivateBinPath (string physicalPath, string[] dirs)
117 int len = dirs.Length;
118 string[] ret = new string [len];
119 for (int i = 0; i < len; i++)
120 ret [i] = Path.Combine (physicalPath, dirs [i]);
121 return String.Join (";", ret);
125 // For further details see `Hosting the ASP.NET runtime'
127 // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
129 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
130 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
132 if (physicalDir == null)
133 throw new NullReferenceException ();
135 // Make sure physicalDir has file system semantics
136 // and not uri semantics ( '\' and not '/' ).
137 physicalDir = Path.GetFullPath (physicalDir);
139 if (hostType == null)
140 throw new ArgumentException ("hostType can't be null");
142 if (virtualDir == null)
143 throw new ArgumentNullException ("virtualDir");
145 Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
150 AppDomainSetup setup = new AppDomainSetup ();
152 setup.ApplicationBase = physicalDir;
154 string webConfig = FindWebConfig (physicalDir);
156 if (webConfig == null)
157 webConfig = Path.Combine (physicalDir, DEFAULT_WEB_CONFIG_NAME);
158 setup.ConfigurationFile = webConfig;
159 setup.DisallowCodeDownload = true;
161 string[] bindirPath = new string [1] { Path.Combine (physicalDir, "bin") };
164 foreach (string dir in HttpApplication.BinDirs) {
165 bindir = Path.Combine (physicalDir, dir);
167 if (Directory.Exists (bindir)) {
168 bindirPath [0] = bindir;
173 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, bindirPath);
174 setup.PrivateBinPathProbe = "*";
175 string dynamic_dir = null;
176 string user = Environment.UserName;
178 string dirPrefix = String.Concat (user, "-temp-aspnet-");
180 for (int i = 0; ; i++){
181 string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
185 string stamp = Path.Combine (d, "stamp");
186 CreateDirectory (stamp);
189 Directory.Delete (stamp);
190 } catch (Exception) {
194 tempDirTag = i.GetHashCode ();
196 } catch (UnauthorizedAccessException){
203 string domain_id = (virtualDir.GetHashCode () + 1 ^ physicalDir.GetHashCode () + 2 ^ tempDirTag).ToString ("x");
205 // This is used by mod_mono's fail-over support
206 string domain_id_suffix = Environment.GetEnvironmentVariable ("__MONO_DOMAIN_ID_SUFFIX");
207 if (domain_id_suffix != null && domain_id_suffix.Length > 0)
208 domain_id += domain_id_suffix;
210 setup.ApplicationName = domain_id;
211 setup.DynamicBase = dynamic_dir;
212 setup.CachePath = dynamic_dir;
214 string dynamic_base = setup.DynamicBase;
215 if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
216 ClearDynamicBaseDirectory (dynamic_base);
222 appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
225 // Populate with the AppDomain data keys expected, Mono only uses a
226 // few, but third party apps might use others:
228 appdomain.SetData (".appDomain", "*");
229 int l = physicalDir.Length;
230 if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
231 physicalDir += Path.DirectorySeparatorChar;
232 appdomain.SetData (".appPath", physicalDir);
233 appdomain.SetData (".appVPath", virtualDir);
234 appdomain.SetData (".appId", domain_id);
235 appdomain.SetData (".domainId", domain_id);
236 appdomain.SetData (".hostingVirtualPath", virtualDir);
237 appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
238 appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
239 appdomain.SetData (MonoHostedDataKey, "yes");
241 appdomain.DoCallBack (SetHostingEnvironment);
242 return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
245 static void SetHostingEnvironment ()
247 bool shadow_copy_enabled = true;
248 HostingEnvironmentSection he = WebConfigurationManager.GetWebApplicationSection ("system.web/hostingEnvironment") as HostingEnvironmentSection;
250 shadow_copy_enabled = he.ShadowCopyBinAssemblies;
252 if (shadow_copy_enabled) {
253 AppDomain current = AppDomain.CurrentDomain;
254 current.SetShadowCopyFiles ();
255 current.SetShadowCopyPath (current.SetupInformation.PrivateBinPath);
258 HostingEnvironment.IsHosted = true;
259 HostingEnvironment.SiteName = HostingEnvironment.ApplicationID;