AppDomainFactory.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // System.Web.Hosting.AppDomainFactory.cs
  3. //
  4. // Authors:
  5. // Bob Smith <[email protected]>
  6. // Gonzalo Paniagua ([email protected])
  7. //
  8. // (C) Bob Smith
  9. // (c) Copyright 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.IO;
  34. using System.Security;
  35. using System.Security.Policy;
  36. using System.Text;
  37. namespace System.Web.Hosting
  38. {
  39. public sealed class AppDomainFactory : IAppDomainFactory
  40. {
  41. static int nDomain = 0;
  42. static string [] domainData = { ".appDomain",
  43. ".appId",
  44. ".appPath",
  45. ".appVPath",
  46. ".appName",
  47. ".domainId"
  48. };
  49. public object Create (string module,
  50. string typeName,
  51. string appId,
  52. string appPath,
  53. string strUrlOfAppOrigin,
  54. int iZone)
  55. {
  56. appPath = Path.GetFullPath (appPath);
  57. if (appPath [appPath.Length - 1] == '\\')
  58. appPath += '\\';
  59. StringBuilder sb = new StringBuilder (appId);
  60. sb.Append ('-');
  61. lock (domainData){
  62. sb.Append (nDomain.ToString ());
  63. nDomain++;
  64. }
  65. sb.Append ('-' + DateTime.Now.ToString ());
  66. string domainId = sb.ToString ();
  67. sb = null;
  68. int slash = appId.IndexOf ('/');
  69. string vPath;
  70. if (slash == -1)
  71. vPath = "/";
  72. else
  73. vPath = appId.Substring (slash + 1);
  74. string appName = (appId.GetHashCode () + appPath.GetHashCode ()).ToString ("x");
  75. AppDomainSetup domainSetup = new AppDomainSetup ();
  76. PopulateDomainBindings (domainId,
  77. appId,
  78. appName,
  79. appPath,
  80. vPath,
  81. domainSetup,
  82. null);
  83. // May be adding more assemblies and such to Evidence?
  84. AppDomain domain = AppDomain.CreateDomain (domainId,
  85. AppDomain.CurrentDomain.Evidence,
  86. domainSetup);
  87. string [] settings = new string [6];
  88. settings [0] = "*";
  89. settings [1] = appId;
  90. settings [2] = appPath;
  91. settings [3] = vPath;
  92. settings [4] = appName;
  93. settings [5] = domainId;
  94. for (int i = 0; i < 6; i++)
  95. domain.SetData (domainData [i], settings [i]);
  96. object o = null;
  97. try {
  98. o = domain.CreateInstance (module, typeName);
  99. } catch {
  100. AppDomain.Unload (domain);
  101. o = null;
  102. }
  103. return o;
  104. }
  105. internal static void PopulateDomainBindings (string domainId,
  106. string appId,
  107. string appName,
  108. string appPath,
  109. string appVPath,
  110. AppDomainSetup setup,
  111. IDictionary dict)
  112. {
  113. setup.PrivateBinPath = "bin";
  114. setup.PrivateBinPathProbe = "*";
  115. setup.ShadowCopyFiles = "true";
  116. //HACK: we should use Uri (appBase).ToString () once Uri works fine.
  117. string uri = "file://" + appPath;
  118. if (Path.DirectorySeparatorChar != '/')
  119. uri = uri.Replace (Path.DirectorySeparatorChar, '/');
  120. setup.ApplicationBase = uri;
  121. setup.ApplicationName = appName;
  122. string temp = Path.GetTempPath ();
  123. setup.DynamicBase = Path.Combine (temp, Environment.UserName + "-temp-aspnet");
  124. string webConfigName = Path.Combine (appPath, "Web.config");
  125. if (File.Exists (webConfigName))
  126. setup.ConfigurationFile = webConfigName;
  127. else
  128. setup.ConfigurationFile = Path.Combine (appPath, "web.config");
  129. if (dict != null) {
  130. dict.Add (domainData [0], "*");
  131. dict.Add (domainData [1], appId);
  132. dict.Add (domainData [2], appPath);
  133. dict.Add (domainData [3], appVPath);
  134. dict.Add (domainData [4], appName);
  135. dict.Add (domainData [5], domainId);
  136. }
  137. }
  138. }
  139. }