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.