1. Asp.net integration unity example
2. ExampleCodeDownload
1. Asp.net integration unity example
1.1 add reference as follows:
1.2 Add the dependency attribute label on the default. aspx page, indicating that the property is generated through dependency injection. As follows:
Private istockquoteservice stockquoteservice;
// Add attribute labels, indicating that attributes need to be injected
[Dependency] Public istockquoteservice stockquoteservice {get {return stockquoteservice;} set {stockquoteservice = value ;}}
1.3 Add the global. asax file, create the unitycontainer in application_start, and destroy the unitycontainer in the application_end function.
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. security; using system. web. sessionstate; namespace stocksticker {using Microsoft. practices. unity; using Microsoft. practices. unity. configuration; using system. web. configuration; using system. web. ui; public class Global: system. web. httpapplication {private const string appcontainerkey = "application container"; private iunitycontainer applicationiner iner {get {return (iunitycontainer) This. application [appcontainerkey];} set {This. application [appcontainerkey] = value ;}}
Protected void application_start (Object sender, eventargs E) { // Create a container Iunitycontainer applicationiner iner = new unitycontainer (); Applicationcontainer. loadconfiguration ("application "); Applicationiner iner = applicationcontainer; } Protected void application_end (Object sender, eventargs e ){
// Destroy the container Iunitycontainer applicationiner = This. applicationcontainer; If (null! = Applicationcontainer) { Applicationcontainer. Dispose (); This. applicationiner iner = NULL; } } // Intercept injection is implemented here, and protected void application_prerequesthandlerexecute (Object sender, eventargs E) is injected into the page ){
Page handler = httpcontext. Current. handler as page; If (null! = Handler) { Iunitycontainer Container = This. applicationcontainer; If (null! = Container) { Container. buildup (handler. GetType (), Handler ); } } }}
}
1.4 modify the Web. xml file, add the configsection configuration node, and register the type with the container. For details, refer to here.
<Section name = "Unity" type = "Microsoft. practices. unity. configuration. unityconfigurationsection, Microsoft. practices. unity. configuration, version = 2.0.414.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "/>
<Unity xmlns = "http://schemas.microsoft.com/practices/2010/unity">
<Alias = "tracesource" type = "system. diagnostics. tracesource, system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "/> <alias =" ilogger "type =" stocksticker. loggers. ilogger, stocksticker "/> <alias =" tracesourcelogger "type =" stocksticker. loggers. tracesourcelogger, stocksticker "/> <alias =" istockquoteservice "type =" stocksticker. stockquoteservices. istockquoteservice, stocksticker "/> <alias =" moneycentralstockquoteservice "type =" stocksticker. stockquoteservices. moneycentralstockquoteservice, stocksticker "/> <container name =" application "> <register type =" ilogger "mapto =" tracesourcelogger "> <lifetime type =" Singleton "/> <constructor> <Param name = "tracesourcename"> <value = "default"/> </param> </constructor> </register> <register type = "istockquoteservice" mapto = "moneycentralstockquoteservice"> <lifetime type = "Singleton"/> <property name = "logger"/> </register> </container> </Unity>
Complete the integration of Asp.net and unity.
2. Download Sample Code
/Files/xuqiang/entlib/aspnetstocksticker.rar