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;
34 namespace System.Web.Hosting {
36 // CAS - no InheritanceDemand here as the class is sealed
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 public sealed class ApplicationHost {
40 static string [] types = { "Web.config", "Web.Config", "web.config" };
42 private ApplicationHost ()
46 static string FindWebConfig (string basedir)
50 foreach (string s in types){
51 r = Path.Combine (basedir, s);
56 // default: return the last one
61 static object create_dir = new object ();
64 static void CreateDirectory (string directory)
68 if (!Directory.Exists (directory))
69 Directory.CreateDirectory (directory);
72 Directory.CreateDirectory (directory);
77 // For further details see `Hosting the ASP.NET runtime'
79 // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
81 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
82 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
84 if (physicalDir == null)
85 throw new NullReferenceException ();
87 // Make sure physicalDir has file system semantics
88 // and not uri semantics ( '\' and not '/' ).
89 physicalDir = Path.GetFullPath (physicalDir);
92 throw new ArgumentException ("hostType can't be null");
94 if (virtualDir == null)
95 throw new ArgumentNullException ("virtualDir");
97 Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
102 string domain_id = (virtualDir + physicalDir).GetHashCode ().ToString ("x");
107 AppDomainSetup setup = new AppDomainSetup ();
109 setup.ApplicationBase = physicalDir;
111 setup.CachePath = null;
112 setup.ApplicationName = domain_id;
113 setup.ConfigurationFile = FindWebConfig (physicalDir);
114 setup.DisallowCodeDownload = true;
115 string bin_path = Path.Combine (physicalDir, "bin");
116 setup.PrivateBinPath = bin_path;
117 setup.PrivateBinPathProbe = "*";
118 setup.ShadowCopyFiles = "true";
119 setup.ShadowCopyDirectories = bin_path;
121 string dynamic_dir = null;
122 string user = Environment.UserName;
123 for (int i = 0; ; i++){
124 string d = Path.Combine (Path.GetTempPath (),
125 String.Format ("{0}-temp-aspnet-{1:x}", user, i));
129 string stamp = Path.Combine (d, "stamp");
130 CreateDirectory (stamp);
132 Directory.Delete (stamp);
134 } catch (UnauthorizedAccessException){
138 setup.DynamicBase = dynamic_dir;
139 CreateDirectory (setup.DynamicBase);
145 appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
148 // Populate with the AppDomain data keys expected, Mono only uses a
149 // few, but third party apps might use others:
151 appdomain.SetData (".appDomain", "*");
152 int l = physicalDir.Length;
153 if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
154 physicalDir += Path.DirectorySeparatorChar;
155 appdomain.SetData (".appPath", physicalDir);
156 appdomain.SetData (".appVPath", virtualDir);
157 appdomain.SetData (".domainId", domain_id);
158 appdomain.SetData (".hostingVirtualPath", virtualDir);
159 appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
161 appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
163 return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);