DAAB Unity & ASP.net

Jul 15, 2008 at 6:14 PM

Hi All,

I'm trying to create a Database object using Unity Integration approach. 

I put IUnityContainer creation in Global.asax.  This is the code (partly derived from David Hayden's Unity Ioc and ASP.NET screencast).  



@ Application Language="C#" %>



@ Import Namespace="Microsoft.Practices.Unity" %>



@ Import Namespace="Microsoft.Practices.Unity.Configuration" %>






script runat="server">






private static IUnityContainer _container;





public static IUnityContainer Container




get { return _container; }



private set { _container = value; }





protected void Application_Start(object sender, EventArgs e)






protected virtual void CreateContainer()




IUnityContainer container = new UnityContainer();



UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");






Container = container;











I am trying to access create a Database object in one of my classes using:




db = HttpContext.Current.ApplicationInstance.Container.Resolve<Database>();



I get this compiler error:

Error 1 'System.Web.HttpApplication' does not contain a definition for 'Container' and no extension method 'Container' accepting a first argument of type 'System.Web.HttpApplication' could be found (are you missing a using directive or an assembly reference?) 

Please help me.





Jul 15, 2008 at 7:04 PM
The error message is telling you exactly what the problem is. The type of HttpContext.Current.ApplicationInstance is HttpApplication. HttpApplication doesn't have a Container property.

Global.asax defines a subclass of HttpApplication. You'll need to cast HttpContext.Current.ApplicationContext to that subclass before you can access the container.

Jul 16, 2008 at 6:10 AM
I added Global.asax.cs.  Added 



class Global : System.Web.HttpApplication

and copied code from Global.asax.  I was able to access the container via Global.Container. 

I remember reading placing Classname="Global" in <%@ Application %> and that didn't work.