Reference: http://shanyou.cnblogs.com/archive/2005/10/28/264103.html
When using the castle IOC container in the project, you can obtain the castle container on the ASP. NET page as follows:
1. Implement icontaineraccessor in global. asax. CS
Global. asax
Public Class Global: system. Web. httpapplication, icontaineraccessor
{
Private Static Windsorcontainer container;
Protected Void Application_start ( Object Sender, eventargs E)
{
// Cachemanager. loadcachefile (); Cache Management class
Container = New Windsorcontainer ( New Xmlinterpreter ( New Configresource ()));
}
Protected Void Application_end ( Object Sender, eventargs E)
{
}
PublicIwindsorcontainer container
{
Get{ReturnContainer ;}
}
}
2. Get the container instance
Containeraccessorutil
Public Class Containeraccessorutil
{
Private Containeraccessorutil ()
{
}
/// <Summary>
/// Obtain all instances in the container
/// </Summary>
/// <Returns> List of all instances </Returns>
Public Static Iwindsorcontainer getcontainer ()
{
Icontaineraccessor containeraccessor = Httpcontext. Current. applicationinstance As Icontaineraccessor;
If (Containeraccessor = Null )
{
Throw New Exception ( " You must extend the httpapplication in your web project " +
" And implement the icontaineraccessor to properly expose your container instance " + " /N " +
" You must implement the icontaineraccessor interface in httpapplication to expose container attributes. " );
}
Iwindsorcontainer container=Containeraccessor. ContainerAsIwindsorcontainer;
If (Container = Null )
{
Throw New Exception ( " The container seems to be unavailable in " +
" Your httpapplication subclass " + " /N " + " Httpapplication cannot get the container instance " );
}
ReturnContainer;
}
}
3. Get the container instance through containeraccessorutil. getcontainer () in the base class of the specific page.
For example:
Getcontainer
Public Isqlmapper sqlmap = (Containeraccessorutil. getcontainer ())[ " Sqlserversqlmap " ] As Isqlmapper;
Public StaticIcontractbiz=Containeraccessorutil. getcontainer ()["Cclog. contractbiz"]AsIcontractbiz;
Ibasicinfobiz=Containeraccessorutil. getcontainer ()["Cclog. basicinfobiz"]AsIbasicinfobiz;
//...
To assign ibatisnet to Castle for management, the isqlmapper instance must also be obtained from the castle container so that castle can truly manage ibatisnet.
How to obtain an isqlmapper instance from a container: basesqlmapdao. CS
Public Isqlmapper sqlmap = (Containeraccessorutil. getcontainer ())[ " Sqlserversqlmap " ] As Isqlmapper;
"Sqlserversqlmap" is specified when we configure ibatisnet facility.
< Facility ID = "Ibatis" Type = "Castle. Facilities. ibatisnetintegration. ibatisnetfacility, Castle. Facilities. ibatisnetintegration" >
< Sqlmap ID = "Sqlserversqlmap" Config = "Sqlmap. config" />
</ Facility >
Appendix:
About the global. asax file:
The global. asax file is sometimes called an ASP. NET application.ProgramFile, provides a way to respond to application-level or module-level events at a central location. You can use this file to implement application security and other tasks.
Global. asax is located in the application root directory. The ASP. NET page framework can automatically identify any changes made to the Global. asax file.
Global. asax contains many events, which are often used for security and can be used to process application-level tasks, such as user authentication, application startup, and user session processing.