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 internal static readonly string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
43 internal static string [] WebConfigFileNames = { "web.config", "Web.config", "Web.Config" };
49 static string FindWebConfig (string basedir)
53 foreach (string s in WebConfigFileNames){
54 r = Path.Combine (basedir, s);
59 // default: return the last one
64 static object create_dir = new object ();
67 internal static bool ClearDynamicBaseDirectory (string directory)
69 string[] entries = null;
72 entries = Directory.GetDirectories (directory);
78 if (entries != null && entries.Length > 0) {
79 foreach (string e in entries) {
80 if (ClearDynamicBaseDirectory (e)) {
91 entries = Directory.GetFiles (directory);
96 if (entries != null && entries.Length > 0) {
97 foreach (string e in entries) {
109 static bool CreateDirectory (string directory)
114 if (!Directory.Exists (directory)) {
115 Directory.CreateDirectory (directory);
124 static string BuildPrivateBinPath (string physicalPath, string[] dirs)
127 int len = dirs.Length;
128 string[] ret = new string [len];
129 for (int i = 0; i < len; i++)
130 ret [i] = Path.Combine (physicalPath, dirs [i]);
131 return String.Join (";", ret);
133 return String.Join (";", dirs);
138 // For further details see `Hosting the ASP.NET runtime'
140 // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
143 [MonoNotSupported ("")]
145 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
146 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
148 if (physicalDir == null)
149 throw new NullReferenceException ();
151 // Make sure physicalDir has file system semantics
152 // and not uri semantics ( '\' and not '/' ).
153 physicalDir = Path.GetFullPath (physicalDir);
155 if (hostType == null)
156 throw new ArgumentException ("hostType can't be null");
158 if (virtualDir == null)
159 throw new ArgumentNullException ("virtualDir");
161 Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
166 AppDomainSetup setup = new AppDomainSetup ();
168 setup.ApplicationBase = physicalDir;
170 setup.ConfigurationFile = FindWebConfig (physicalDir);
171 setup.DisallowCodeDownload = true;
173 string[] bindirPath = new string [1] {
175 Path.Combine (physicalDir, "bin")
182 foreach (string dir in HttpApplication.BinDirs) {
183 bindir = Path.Combine (physicalDir, dir);
185 if (Directory.Exists (bindir)) {
187 bindirPath [0] = bindir;
189 bindirPath [0] = dir;
195 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, bindirPath);
196 setup.PrivateBinPathProbe = "*";
197 string dynamic_dir = null;
198 string user = Environment.UserName;
200 string dirPrefix = String.Concat (user, "-temp-aspnet-");
202 for (int i = 0; ; i++){
203 string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
207 string stamp = Path.Combine (d, "stamp");
208 CreateDirectory (stamp);
211 Directory.Delete (stamp);
212 } catch (Exception) {
216 tempDirTag = i.GetHashCode ();
218 } catch (UnauthorizedAccessException){
225 string domain_id = (virtualDir.GetHashCode () + 1 ^ physicalDir.GetHashCode () + 2 ^ tempDirTag).ToString ("x");
227 // This is used by mod_mono's fail-over support
228 string domain_id_suffix = Environment.GetEnvironmentVariable ("__MONO_DOMAIN_ID_SUFFIX");
229 if (domain_id_suffix != null && domain_id_suffix.Length > 0)
230 domain_id += domain_id_suffix;
232 setup.ApplicationName = domain_id;
233 setup.DynamicBase = dynamic_dir;
234 setup.CachePath = dynamic_dir;
236 string dynamic_base = setup.DynamicBase;
237 if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
238 ClearDynamicBaseDirectory (dynamic_base);
244 appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
247 // Populate with the AppDomain data keys expected, Mono only uses a
248 // few, but third party apps might use others:
250 appdomain.SetData (".appDomain", "*");
251 int l = physicalDir.Length;
252 if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
253 physicalDir += Path.DirectorySeparatorChar;
254 appdomain.SetData (".appPath", physicalDir);
255 appdomain.SetData (".appVPath", virtualDir);
256 appdomain.SetData (".domainId", domain_id);
257 appdomain.SetData (".hostingVirtualPath", virtualDir);
258 appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
260 appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
261 HostingEnvironment.SetIsHosted (false);
263 appdomain.SetData (MonoHostedDataKey, "yes");
266 appdomain.DoCallBack (SetHostingEnvironment);
268 return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
272 static void SetHostingEnvironment ()
274 bool shadow_copy_enabled = true;
275 HostingEnvironmentSection he = WebConfigurationManager.GetWebApplicationSection ("system.web/hostingEnvironment") as HostingEnvironmentSection;
277 shadow_copy_enabled = he.ShadowCopyBinAssemblies;
279 if (shadow_copy_enabled) {
280 AppDomain current = AppDomain.CurrentDomain;
281 current.SetShadowCopyFiles ();
282 current.SetShadowCopyPath (current.SetupInformation.PrivateBinPath);