* IAppDomainFactory.cs: added missing marshalling attributes
[mono.git] / mcs / class / System.Web / System.Web.Hosting / ApplicationHost.cs
1 //
2 // System.Web.Hosting.ApplicationHost
3 //
4 // Authors:
5 //      Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //      (class signature from Bob Smith <bob@thestuff.net> (C) )
7 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 //
9 using System;
10 using System.Collections;
11 using System.IO;
12 using System.Runtime.Remoting;
13 using System.Web.Util;
14
15 namespace System.Web.Hosting
16 {
17         public sealed class ApplicationHost
18         {
19                 internal class ConfigInitHelper : MarshalByRefObject
20                 {
21                         internal void InitConfig ()
22                         {
23                         }
24                 }
25                 
26                 private ApplicationHost ()
27                 {
28                 }
29
30                 public static object CreateApplicationHost (Type hostType,
31                                                             string virtualDir,
32                                                             string physicalDir)
33                 {
34                         if (hostType == null)
35                                 throw new ArgumentException ("hostType");
36
37                         if (virtualDir == null || virtualDir.Length == 0)
38                                 throw new ArgumentException ("virtualDir");
39                         
40                         if (physicalDir == null || physicalDir.Length == 0)
41                                 throw new ArgumentException ("physicalDir");
42
43                         if (physicalDir [physicalDir.Length - 1] != Path.DirectorySeparatorChar)
44                                 physicalDir += Path.DirectorySeparatorChar;
45
46                         int nowInt = DateTime.Now.ToString ().GetHashCode ();
47                         string nowHash = nowInt.ToString ("x");
48                         nowInt += physicalDir.GetHashCode ();
49                         string sum = nowInt.ToString ("x");
50                         Hashtable hTable = new Hashtable ();
51                         AppDomainSetup domainSetup = new AppDomainSetup();
52
53                         AppDomainFactory.PopulateDomainBindings (nowHash,
54                                                                  sum,
55                                                                  sum,
56                                                                  physicalDir,
57                                                                  virtualDir,
58                                                                  domainSetup,
59                                                                  hTable);
60                         
61                         AppDomain domain = AppDomain.CreateDomain (nowHash, null, domainSetup);
62                         foreach (string key in hTable.Keys)
63                                 domain.SetData (key, (string) hTable [key]);
64
65                         domain.SetData (".hostingVirtualPath", virtualDir);
66                         domain.SetData (".hostingInstallDir", ICalls.GetMachineInstallDirectory ());
67                         InitConfigInNewAppDomain (domain);
68                         ObjectHandle o = domain.CreateInstance (hostType.Assembly.FullName,
69                                                                 hostType.FullName);
70                         return o.Unwrap();
71                 }
72
73                 private static void InitConfigInNewAppDomain (AppDomain appDomain)
74                 {
75                         Type t = typeof (ConfigInitHelper);
76                         ObjectHandle o = appDomain.CreateInstance (t.Assembly.FullName, t.FullName);
77                         ConfigInitHelper helper = (ConfigInitHelper) o.Unwrap();
78                         helper.InitConfig ();
79                 }
80         }
81 }
82