Using AUTOFAC in the ASP. NET Web forms/mvc/console app

Source: Internet
Author: User

Originally introduced the AUTOFAC in the ASP. NET Web Forms application, later added the MVC, the console application uses the AUTOFAC, details please see the source code .

ASP. NET Web forms uses AUTOFAC, which requires at least one step:

1, referencing the AUTOFAC assembly.

2. Add the Autofac Web Modules. config.

3, implement the Icontainerprovideraccessor interface in Global.asax.

We create an ASP. NET Web forms project named Webformstudy.

Add Reference

The simplest way to add references is to use NuGet, right-click the references under the Webformstudy project, and select Manage NuGet Packages, such as:

Enter the Auto.web word in search online, Autofac WebForms intergration to search, click Install.

Once installed, we can see the addition of Autofac.dll and Autofac.Integration.Web.dll in references, such as:

Add Modules to the Web. config

AUTOFAC manages the lifecycle of the component and adds dependency injection to the ASP. IHttpModule implementation via the (note: During HttpApplication initialization, The corresponding HttpModule object that implements the IHttpModule interface is loaded and initialized according to the configuration file. For HttpApplication, when it handles different stages of an HTTP request, it triggers different events, and HttpModule's meaning is to inject the required action into the process of the entire HTTP request by registering the appropriate event for the HttpApplication. Many of the features of ASP, such as authentication, authorization, and caching, are implemented through the corresponding HttpModule. Excerpt from: ASP. NET Mvc4 framework), you need to configure these modules in Web. config.

Fortunately, if you add the AUTOFAC assembly through NuGet, the corresponding modules is automatically configured in Web. config When you install it, such as:

Implement Icontainerprovideraccessor interface in GLOBAL.ASZX

The dependency injection module requires the HttpApplication instance to implement the Icontainerprovideraccessor interface. A complete global application class is as follows:

public class Global:httpapplication,icontainerprovideraccessor

{

Static Icontainerprovider _containerprovider;

Public Icontainerprovider Containerprovider

{

get {return _containerprovider;}

}

void Application_Start (object sender, EventArgs e)

{

Code that runs on application startup

Routeconfig.registerroutes (routetable.routes);

Bundleconfig.registerbundles (Bundletable.bundles);

#region The code we added

var builder = new Containerbuilder ();

Registering a component that will be created by reflection

Builder. Registertype<databasemanager> ();

Builder. Registertype<oracledatabase> (). As<idatabase> ();

_containerprovider = new Containerprovider (builder. Build ());

#endregion

}

}

Databasemanager, Oracledatabase and other class code:

  Public InterfaceIdatabase {stringName {Get; } stringSelect (stringCommandText); stringInsert (stringCommandText); stringUpdate (stringCommandText); stringDelete (stringCommandText); }
Idatabase
  Public classDatabasemanager {idatabase _database;  PublicDatabasemanager (idatabase database) {_database=database; }         Public stringSearch (stringCommandText) {            return_database.        Select (CommandText); }         Public stringADD (stringCommandText) {            return_database.        Insert (CommandText); }         Public stringSave (stringCommandText) {            return_database.        Update (CommandText); }         Public stringRemove (stringCommandText) {            return_database.        Delete (CommandText); }    }
Databasemanager
  Public classSqldatabase:idatabase { Public stringName {Get{return "SQL Server"; } }         Public stringSelect (stringCommandText) {            return string. Format ("' {0} ' is a query for SQL in {1}!", CommandText, Name); }         Public stringInsert (stringCommandText) {           return  string. Format ("' {0} ' is a inserts SQL in {1}!", CommandText, Name); }         Public stringUpdate (stringCommandText) {            return  string. Format ("' {0} ' is a update for SQL in {1}!", CommandText, Name); }         Public stringDelete (stringCommandText) {            return  string. Format ("' {0} ' is a delete sql in {1}!", CommandText, Name); }    } 
SQLDatabase
     Public classOracledatabase:idatabase { Public stringName {Get{return "Oracle"; } }         Public stringSelect (stringCommandText) {            return string. Format ("' {0} ' is a query for SQL in {1}!", CommandText, Name); }         Public stringInsert (stringCommandText) {            return  string. Format ("' {0} ' is a inserts SQL in {1}!", CommandText, Name); }         Public stringUpdate (stringCommandText) {           return  string. Format ("' {0} ' is a update for SQL in {1}!", CommandText, Name); }         Public stringDelete (stringCommandText) {            return  string. Format ("' {0} ' is a delete sql in {1}!", CommandText, Name); }    }
Oracledatabase

Run, bad, error, such as:

No problem, note the following configuration information:

Run again, OK, such as:

Source Address

Reference: http://www.cnblogs.com/liping13599168/archive/2011/07/16/2108209.html

Related Article

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.