WebAPI2 uses AUTOFAC dependency injection into a complete solution.

Source: Internet
Author: User

WebApi2 on the dependency injection, Baidu can search the complete solution of the article less and less, lack of arm broken leg.

and MVC5 depending on the injection, and where it needs attention, is marked in the comments. On the Global Code:

Namespace S2s.        webapi{public class WebApiApplication:System.Web.HttpApplication {protected void Application_Start ()            {//----AUTOFAC DI------var builder = new Containerbuilder ();            Setupresolverules (builder); Builder.               Registerapicontrollers (assembly.getexecutingassembly ()); Registerapicontrollers method var container = Builder.            Build (); httpconfiguration config = globalconfiguration.configuration;//Note here that the Config object of the Httpconfiguration class must not be new, To get config from Globalconfiguration.      Dependencyresolver = (new Autofacwebapidependencyresolver (container)); Note that this is different from the MVC Dependency Injection//---------------------------------------------------------arearegistration.regist            Erallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);                 Bundleconfig.registerbundles (Bundletable.bundles);       Arearegistration.registerallareas ();            Globalconfiguration.configure (Webapiconfig.register);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);        Bundleconfig.registerbundles (Bundletable.bundles); }//<summary>//AUTOFAC//</summary>/<param name= "Builder" ></ param> private static void Setupresolverules (Containerbuilder builder) {var assembly = Assemb Ly. Load ("S2s.   BLL "); Loads the assembly builder based on the assembly name. Registerassemblytypes (assembly). SingleInstance ();//Returns the same instance builder every time. Registerassemblytypes (assembly). Where (t = = T.name.endswith ("Service")).        Asimplementedinterfaces (); }    }}

There's not much that needs to be explained, and the controller is still injected into the constructor. The development has been pro-test effective.

Can be collected and viewed later.

WebAPI2 uses AUTOFAC dependency injection into a complete solution.

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.