AppDomainFactory.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Security;
  15. using System.Security.Policy;
  16. using System.Text;
  17. namespace System.Web.Hosting
  18. {
  19. public sealed class AppDomainFactory : IAppDomainFactory
  20. {
  21. static int nDomain = 0;
  22. static string [] domainData = { ".appDomain",
  23. ".appId",
  24. ".appPath",
  25. ".appVPath",
  26. ".appName",
  27. ".domainId"
  28. };
  29. public object Create (string module,
  30. string typeName,
  31. string appId,
  32. string appPath,
  33. string strUrlOfAppOrigin,
  34. int iZone)
  35. {
  36. appPath = Path.GetFullPath (appPath);
  37. if (appPath [appPath.Length - 1] == '\\')
  38. appPath += '\\';
  39. StringBuilder sb = new StringBuilder (appId);
  40. sb.Append ('-');
  41. lock (domainData){
  42. sb.Append (nDomain.ToString ());
  43. nDomain++;
  44. }
  45. sb.Append ('-' + DateTime.Now.ToString ());
  46. string domainId = sb.ToString ();
  47. sb = null;
  48. int slash = appId.IndexOf ('/');
  49. string vPath;
  50. if (slash == -1)
  51. vPath = "/";
  52. else
  53. vPath = appId.Substring (slash + 1);
  54. string appName = (appId.GetHashCode () + appPath.GetHashCode ()).ToString ("x");
  55. AppDomainSetup domainSetup = new AppDomainSetup ();
  56. PopulateDomainBindings (domainId,
  57. appId,
  58. appName,
  59. appPath,
  60. vPath,
  61. domainSetup,
  62. null);
  63. // May be adding more assemblies and such to Evidence?
  64. AppDomain domain = AppDomain.CreateDomain (domainId,
  65. AppDomain.CurrentDomain.Evidence,
  66. domainSetup);
  67. string [] settings = new string [6];
  68. settings [0] = "*";
  69. settings [1] = appId;
  70. settings [2] = appPath;
  71. settings [3] = vPath;
  72. settings [4] = appName;
  73. settings [5] = domainId;
  74. for (int i = 0; i < 6; i++)
  75. domain.SetData (domainData [i], settings [i]);
  76. object o = null;
  77. try {
  78. o = domain.CreateInstance (module, typeName);
  79. } catch {
  80. AppDomain.Unload (domain);
  81. o = null;
  82. }
  83. return o;
  84. }
  85. internal static void PopulateDomainBindings (string domainId,
  86. string appId,
  87. string appName,
  88. string appPath,
  89. string appVPath,
  90. AppDomainSetup setup,
  91. IDictionary dict)
  92. {
  93. setup.PrivateBinPath = "bin";
  94. setup.PrivateBinPathProbe = "*";
  95. setup.ShadowCopyFiles = "true";
  96. //HACK: we should use Uri (appBase).ToString () once Uri works fine.
  97. string uri = "file://" + appPath;
  98. if (Path.DirectorySeparatorChar != '/')
  99. uri = uri.Replace (Path.DirectorySeparatorChar, '/');
  100. setup.ApplicationBase = uri;
  101. setup.ApplicationName = appName;
  102. string webConfigName = Path.Combine (appPath, "Web.config");
  103. if (File.Exists (webConfigName))
  104. setup.ConfigurationFile = webConfigName;
  105. else
  106. setup.ConfigurationFile = Path.Combine (appPath, "web.config");
  107. if (dict != null) {
  108. dict.Add (domainData [0], "*");
  109. dict.Add (domainData [1], appId);
  110. dict.Add (domainData [2], appPath);
  111. dict.Add (domainData [3], appVPath);
  112. dict.Add (domainData [4], appName);
  113. dict.Add (domainData [5], domainId);
  114. }
  115. }
  116. }
  117. }