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.
31 using System.Security.Permissions;
32 using System.Security.Policy;
35 namespace System.Web.Hosting {
37 // CAS - no InheritanceDemand here as the class is sealed
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 public sealed class ApplicationHost {
40 internal static readonly string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
41 internal static string [] WebConfigFileNames = { "Web.config", "Web.Config", "web.config" };
43 private ApplicationHost ()
47 static string FindWebConfig (string basedir)
51 foreach (string s in WebConfigFileNames){
52 r = Path.Combine (basedir, s);
57 // default: return the last one
62 static object create_dir = new object ();
65 internal static bool ClearDynamicBaseDirectory (string directory)
67 string[] entries = null;
70 entries = Directory.GetDirectories (directory);
76 if (entries != null && entries.Length > 0) {
77 foreach (string e in entries) {
78 if (ClearDynamicBaseDirectory (e)) {
89 entries = Directory.GetFiles (directory);
94 if (entries != null && entries.Length > 0) {
95 foreach (string e in entries) {
107 static bool CreateDirectory (string directory)
112 if (!Directory.Exists (directory)) {
113 Directory.CreateDirectory (directory);
122 static string BuildPrivateBinPath (string physicalPath, string[] dirs)
125 int len = dirs.Length;
126 string[] ret = new string [len];
127 for (int i = 0; i < len; i++)
128 ret [i] = Path.Combine (physicalPath, dirs [i]);
129 return String.Join (";", ret);
131 return String.Join (";", dirs);
136 // For further details see `Hosting the ASP.NET runtime'
138 // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
141 [MonoNotSupported ("")]
143 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
144 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
146 if (physicalDir == null)
147 throw new NullReferenceException ();
149 // Make sure physicalDir has file system semantics
150 // and not uri semantics ( '\' and not '/' ).
151 physicalDir = Path.GetFullPath (physicalDir);
153 if (hostType == null)
154 throw new ArgumentException ("hostType can't be null");
156 if (virtualDir == null)
157 throw new ArgumentNullException ("virtualDir");
159 Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
164 AppDomainSetup setup = new AppDomainSetup ();
166 setup.ApplicationBase = physicalDir;
168 setup.CachePath = null;
169 setup.ConfigurationFile = FindWebConfig (physicalDir);
170 setup.DisallowCodeDownload = true;
172 string[] bindirPath = new string [1] {
174 Path.Combine (physicalDir, "bin")
181 foreach (string dir in HttpApplication.BinDirs) {
182 bindir = Path.Combine (physicalDir, dir);
184 if (Directory.Exists (bindir)) {
186 bindirPath [0] = bindir;
188 bindirPath [0] = dir;
194 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, HttpApplication.BinDirs);
195 setup.PrivateBinPathProbe = "*";
196 setup.ShadowCopyFiles = "true";
197 setup.ShadowCopyDirectories = setup.PrivateBinPath;
199 string dynamic_dir = null;
200 string user = Environment.UserName;
202 string dirPrefix = String.Concat (user, "-temp-aspnet-");
204 for (int i = 0; ; i++){
205 string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
209 string stamp = Path.Combine (d, "stamp");
210 CreateDirectory (stamp);
213 Directory.Delete (stamp);
214 } catch (Exception) {
218 tempDirTag = i.GetHashCode ();
220 } catch (UnauthorizedAccessException){
227 string domain_id = (virtualDir.GetHashCode () + 1 ^ physicalDir.GetHashCode () + 2 ^ tempDirTag).ToString ("x");
229 setup.ApplicationName = domain_id;
230 setup.DynamicBase = dynamic_dir;
232 string dynamic_base = setup.DynamicBase;
233 if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
234 ClearDynamicBaseDirectory (dynamic_base);
240 appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
243 // Populate with the AppDomain data keys expected, Mono only uses a
244 // few, but third party apps might use others:
246 appdomain.SetData (".appDomain", "*");
247 int l = physicalDir.Length;
248 if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
249 physicalDir += Path.DirectorySeparatorChar;
250 appdomain.SetData (".appPath", physicalDir);
251 appdomain.SetData (".appVPath", virtualDir);
252 appdomain.SetData (".domainId", domain_id);
253 appdomain.SetData (".hostingVirtualPath", virtualDir);
254 appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
256 appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
258 appdomain.SetData (MonoHostedDataKey, "yes");
260 return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);