2 // System.Web.Hosting.BareApplicationHost
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
34 namespace System.Web.Hosting {
35 class RegisteredItem {
36 public IRegisteredObject Item;
37 public bool AutoClean;
38 public RegisteredItem (IRegisteredObject item, bool autoclean)
41 this.AutoClean = autoclean;
45 sealed class BareApplicationHost : MarshalByRefObject {
48 Dictionary<Type, RegisteredItem> hash;
49 internal ApplicationManager Manager;
50 internal string AppID;
52 public BareApplicationHost ()
59 hash = new Dictionary<Type, RegisteredItem> ();
60 HostingEnvironment.Host = this;
61 AppDomain current = AppDomain.CurrentDomain;
62 current.DomainUnload += OnDomainUnload;
63 phys_path = (string) current.GetData (".appPath");
64 vpath = (string) current.GetData (".appVPath");
67 public string VirtualPath {
71 public string PhysicalPath {
72 get { return phys_path; }
75 public AppDomain Domain {
76 get { return AppDomain.CurrentDomain; }
79 public void Shutdown ()
81 HostingEnvironment.InitiateShutdown ();
84 public void StopObject (Type type)
86 if (!hash.ContainsKey (type))
89 RegisteredItem reg = hash [type];
90 reg.Item.Stop (false);
93 public IRegisteredObject CreateInstance (Type type)
95 return (IRegisteredObject) Activator.CreateInstance (type, null);
98 public void RegisterObject (IRegisteredObject obj, bool auto_clean)
100 hash [obj.GetType ()] = new RegisteredItem (obj, auto_clean);
103 public bool UnregisterObject (IRegisteredObject obj)
105 return hash.Remove (obj.GetType ());
108 public IRegisteredObject GetObject (Type type)
110 if (hash.ContainsKey (type))
111 return hash [type].Item;
116 public string GetCodeGenDir ()
118 return AppDomain.CurrentDomain.SetupInformation.DynamicBase;
121 void OnDomainUnload (object sender, EventArgs args)
123 Manager.RemoveHost (AppID);
124 ICollection<RegisteredItem> values = hash.Values;
125 RegisteredItem [] objects = new RegisteredItem [hash.Count];
126 values.CopyTo (objects, 0);
128 foreach (RegisteredItem reg in objects) {
130 reg.Item.Stop (true); // Stop should call Unregister. It's ok if not.