* IAppDomainFactory.cs: added missing marshalling attributes
[mono.git] / mcs / class / System.Web / System.Web.Hosting / AppDomainFactory.cs
1 //
2 // System.Web.Hosting.AppDomainFactory.cs
3 //
4 // Authors:
5 //      Bob Smith <bob@thestuff.net>
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //
8 // (C) Bob Smith
9 // (c) Copyright 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
18 namespace System.Web.Hosting
19 {
20         public sealed class AppDomainFactory : IAppDomainFactory
21         {
22                 static int nDomain = 0;
23                 static string [] domainData = { ".appDomain",
24                                                 ".appId",
25                                                 ".appPath",
26                                                 ".appVPath",
27                                                 ".appName",
28                                                 ".domainId"
29                                                 };
30
31                 public object Create (string module,
32                                       string typeName,
33                                       string appId,
34                                       string appPath,
35                                       string strUrlOfAppOrigin,
36                                       int iZone)
37                 {
38                         appPath = Path.GetFullPath (appPath);
39                         if (appPath [appPath.Length - 1] == '\\')
40                                 appPath += '\\';
41
42                         StringBuilder sb = new StringBuilder (appId);
43                         sb.Append ('-');
44                         lock (domainData){
45                                 sb.Append (nDomain.ToString ());
46                                 nDomain++;
47                         }
48
49                         sb.Append ('-' + DateTime.Now.ToString ());
50                         string domainId = sb.ToString ();
51                         sb = null;
52
53                         int slash = appId.IndexOf ('/');
54                         string vPath;
55                         if (slash == -1)
56                                 vPath = "/";
57                         else
58                                 vPath = appId.Substring (slash + 1);
59
60                         string appName = (appId.GetHashCode () + appPath.GetHashCode ()).ToString ("x");
61                         AppDomainSetup domainSetup = new AppDomainSetup ();
62
63                         PopulateDomainBindings (domainId,
64                                                 appId,
65                                                 appName,
66                                                 appPath,
67                                                 vPath,
68                                                 domainSetup,
69                                                 null);
70
71                         // May be adding more assemblies and such to Evidence?
72                         AppDomain domain = AppDomain.CreateDomain (domainId,
73                                                                    AppDomain.CurrentDomain.Evidence,
74                                                                    domainSetup);
75
76                         string [] settings = new string [6];
77                         settings [0] = "*";
78                         settings [1] = appId;
79                         settings [2] = appPath;
80                         settings [3] = vPath;
81                         settings [4] = appName;
82                         settings [5] = domainId;
83                         for (int i = 0; i < 6; i++)
84                                 domain.SetData (domainData [i], settings [i]);
85
86                         object o = null;
87                         try {
88                                 o = domain.CreateInstance (module, typeName);
89                         } catch {
90                                 AppDomain.Unload (domain);
91                                 o = null;
92                         }
93
94                         return o;
95                 }
96
97                 internal static void PopulateDomainBindings (string domainId,
98                                                              string appId,
99                                                              string appName,
100                                                              string appPath,
101                                                              string appVPath,
102                                                              AppDomainSetup setup,
103                                                              IDictionary dict)
104                 {
105                         setup.PrivateBinPath = "bin";
106                         setup.PrivateBinPathProbe = "*";
107                         setup.ShadowCopyFiles = "true";
108                         
109                         //HACK: we should use Uri (appBase).ToString () once Uri works fine.
110                         string uri = "file://" + appPath;
111                         if (Path.DirectorySeparatorChar != '/')
112                                 uri = uri.Replace (Path.DirectorySeparatorChar, '/');
113                                 
114                         setup.ApplicationBase = uri;
115                         setup.ApplicationName = appName;
116                         string temp = Path.GetTempPath ();
117                         setup.DynamicBase = Path.Combine (temp, Environment.UserName + "-temp-aspnet");
118                         string webConfigName = Path.Combine (appPath, "Web.config");
119                         if (File.Exists (webConfigName))
120                                 setup.ConfigurationFile = webConfigName;
121                         else
122                                 setup.ConfigurationFile = Path.Combine (appPath, "web.config");
123
124                         if (dict != null) {
125                                 dict.Add (domainData [0], "*");
126                                 dict.Add (domainData [1], appId);
127                                 dict.Add (domainData [2], appPath);
128                                 dict.Add (domainData [3], appVPath);
129                                 dict.Add (domainData [4], appName);
130                                 dict.Add (domainData [5], domainId);
131                         }
132                 }
133         }
134 }
135