Entlib 5.0 learning Notes use unity in Asp.net

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.