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" };
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 // For further details see `Hosting the ASP.NET runtime'
63 // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
65 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
66 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
68 if (physicalDir == null)
69 throw new NullReferenceException ();
72 physicalDir = Path.GetFullPath (physicalDir);
75 Uri u = new Uri (physicalDir);
76 physicalDir = HttpUtility.UrlDecode (u.AbsolutePath);
79 throw new NullReferenceException ();
81 if (virtualDir == null)
82 throw new NullReferenceException ();
84 Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
89 string domain_id = "ASPHOST_" + DateTime.Now.ToString().GetHashCode().ToString("x");
94 AppDomainSetup setup = new AppDomainSetup ();
96 setup.ApplicationBase = physicalDir;
98 setup.CachePath = null;
99 setup.ApplicationName = domain_id;
100 setup.ConfigurationFile = FindWebConfig (physicalDir);
101 setup.DisallowCodeDownload = true;
102 string bin_path = Path.Combine (physicalDir, "bin");
103 setup.PrivateBinPath = bin_path;
104 setup.PrivateBinPathProbe = "*";
105 setup.ShadowCopyFiles = "true";
106 setup.ShadowCopyDirectories = bin_path;
108 string dynamic_dir = null;
109 string user = Environment.UserName;
110 for (int i = 0; ; i++){
111 string d = Path.Combine (Path.GetTempPath (),
112 String.Format ("{0}-temp-aspnet-{1:x}", user, i));
115 Directory.CreateDirectory (d);
116 string stamp = Path.Combine (d, "stamp");
117 Directory.CreateDirectory (stamp);
119 Directory.Delete (stamp);
121 } catch (UnauthorizedAccessException){
125 setup.DynamicBase = dynamic_dir;
126 Directory.CreateDirectory (setup.DynamicBase);
132 appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
135 // Populate with the AppDomain data keys expected, Mono only uses a
136 // few, but third party apps might use others:
138 appdomain.SetData (".appDomain", "*");
139 int l = physicalDir.Length;
140 if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
141 physicalDir += Path.DirectorySeparatorChar;
142 appdomain.SetData (".appPath", physicalDir);
143 appdomain.SetData (".appVPath", virtualDir);
144 appdomain.SetData (".domainId", domain_id);
145 appdomain.SetData (".hostingVirtualPath", virtualDir);
146 appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
148 return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);