. NET MVC AUTOFAC Foundation Environment Building

Source: Internet
Author: User

Run a command installation reference under NuGet

Install-package autofac-version 3.1.0

Install-package Autofac.mvc4

        public static void Registerautofac ()        {            Containerbuilder builder = new Containerbuilder ();            If you need to register all by default, please write this (the main parameter needs to be modified)            //builder. Registercontrollers (assembly.getexecutingassembly ());            #region IOC Register area            //Get base class            var baseType = typeof (Idependency);            Get all the types of the current application            var Assmblys = AppDomain.CurrentDomain.GetAssemblies (). ToList ();            Builder. Registercontrollers (Assmblys. ToArray ());            If the current application is related to the base class, register.            Builder. Registerassemblytypes (Assmblys. ToArray ())                . Where (t = basetype.isassignablefrom (t) && t! = baseType)                . Asimplementedinterfaces (). Instanceperdependency ();            #endregion            var container = Builder. Build ();            Dependencyresolver.setresolver (new Autofacdependencyresolver (container));        }
AUTOFAC Automatic registration code, when the project is successfully launched, it can be changed to manual registration to speed up efficiency


Dependency injection can be implemented by invoking the auto-registration code in Global.aspx.cs
REGISTERAUTOFAC ();

Finally, the use of the constructor function in the controllers to use the online already available examples are no longer a simple environment to build success




. NET MVC AUTOFAC Foundation Environment Building

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.