ApplicationHost.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // System.Web.Hosting.ApplicationHost.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Security.Policy;
  32. namespace System.Web.Hosting {
  33. public sealed class ApplicationHost {
  34. static string [] types = { "Web.config", "web.config" };
  35. private ApplicationHost ()
  36. {
  37. }
  38. static string FindWebConfig (string basedir)
  39. {
  40. string r = null;
  41. foreach (string s in types){
  42. r = Path.Combine (basedir, s);
  43. if (File.Exists (r))
  44. return r;
  45. }
  46. // default: return the last one
  47. return r;
  48. }
  49. //
  50. // For furthe details see `Hosting the ASP.NET runtime'
  51. //
  52. // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
  53. //
  54. public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
  55. {
  56. if (physicalDir == null)
  57. throw new NullReferenceException ();
  58. #pragma warning disable 219
  59. //
  60. // This is done just for validation: it might throw an exception
  61. //
  62. Uri u = new Uri (physicalDir);
  63. #pragma warning restore 219
  64. if (hostType == null)
  65. throw new NullReferenceException ();
  66. if (virtualDir == null)
  67. throw new NullReferenceException ();
  68. if (hostType == null || virtualDir == null || physicalDir == null)
  69. throw new NullReferenceException ();
  70. Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
  71. //
  72. // Unique Domain ID
  73. //
  74. string domain_id = "ASPHOST_" + DateTime.Now.ToString().GetHashCode().ToString("x");
  75. //
  76. // Setup
  77. //
  78. AppDomainSetup setup = new AppDomainSetup ();
  79. setup.ApplicationBase = physicalDir;
  80. setup.CachePath = null;
  81. setup.ApplicationName = domain_id;
  82. setup.ConfigurationFile = FindWebConfig (physicalDir);
  83. setup.DisallowCodeDownload = true;
  84. setup.PrivateBinPath = "bin";
  85. setup.PrivateBinPathProbe = "*";
  86. setup.ShadowCopyFiles = "true";
  87. UriBuilder b = new UriBuilder ("file://", null, 0, Path.Combine (physicalDir, "bin"));
  88. setup.ShadowCopyDirectories = b.Uri.ToString ();
  89. string dynamic_dir = null;
  90. string user = Environment.UserName;
  91. for (int i = 0; ; i++){
  92. string d = Path.Combine (Path.GetTempPath (),
  93. String.Format ("{0}-temp-aspnet-{1:x}", user, i));
  94. try {
  95. Directory.CreateDirectory (d);
  96. string stamp = Path.Combine (d, "stamp");
  97. Directory.CreateDirectory (stamp);
  98. dynamic_dir = d;
  99. Directory.Delete (stamp);
  100. break;
  101. } catch (UnauthorizedAccessException){
  102. continue;
  103. }
  104. }
  105. setup.DynamicBase = dynamic_dir;
  106. Directory.CreateDirectory (setup.DynamicBase);
  107. //
  108. // Create app domain
  109. //
  110. AppDomain appdomain;
  111. appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
  112. //
  113. // Populate with the AppDomain data keys expected, Mono only uses a
  114. // few, but third party apps might use others:
  115. //
  116. appdomain.SetData (".appDomain", "*");
  117. appdomain.SetData (".appPath", physicalDir);
  118. appdomain.SetData (".appVPath", virtualDir);
  119. appdomain.SetData (".domainId", domain_id);
  120. appdomain.SetData (".hostingVirtualPath", virtualDir);
  121. appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
  122. return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
  123. }
  124. }
  125. }