Today we try to achieve AUTOFAC injection in WebApi2, about this aspect is also to see a few friends of the sharing saved a lot of time, so combined with the previous chapter of the demo and then new Webapi ...
Demo3:http://pan.baidu.com/s/1escaztc
The same as the beginning or the introduction of the approximate demo structure:
or NuGet installs AUTOFAC and AUTOFAC ASP. NET MVC 5 integration, AUTOFAC ASP. NET WEB API 2.2 integration
The official recommendation is to use a constructor to inject a friend who says there is a security risk to attribute injection.
Deliberately find the relevant content, but not sure whether the point of view is correct, there are clear friends trouble can be informed, I also good to add to let more friends understand ha, in this thanks.
Advantages and Disadvantages
1. Attribute injection is straightforward, the disadvantage is that when it comes to attributes, many constructors appear to be bloated.
2. Construction injection is a high-cohesion embodiment, especially for some attributes that need to be assigned when the object is created, and subsequent modifications are not allowed (no setter method is provided).
No wordy, mainly to paste the WebApi2 global file, which focuses on the MVC and WEBAPI registration of the ground (red part)
#regionAutomatic injection//Create a container instance of the AUTOFAC management registration class varBuilder =NewContainerbuilder (); httpconfiguration config = globalconfiguration.configuration; Assembly[] Assemblies= Directory.GetFiles (AppDomain.CurrentDomain.RelativeSearchPath,"*.dll"). Select (Assembly.LoadFrom). ToArray (); //register all types that implement the Idependency interfaceType BaseType =typeof(idependency); Builder. Registerassemblytypes (Assemblies). Where (Type= Basetype.isassignablefrom (type) &&!type. IsAbstract). Asself (). Asimplementedinterfaces (). Propertiesautowired (). Instanceperlifetimescope (); //Register MVC type//builder. Registercontrollers (Assemblies). Propertiesautowired (); Register API Type Builder. Registerapicontrollers (Assemblies). Propertiesautowired (); //Builder. Registerfilterprovider ();Builder. Registerwebapifilterprovider (config); varcontainer =Builder. Build (); //Registering API containers requires the use of the Httpconfiguration object config. Dependencyresolver = new Autofacwebapidependencyresolver (container); //Registration ParsingDependencyresolver.setresolver (NewAutofacdependencyresolver (container)); #endregion
New Add TestController
[Routeprefix ("api/test")] Public classTestcontroller:apicontroller {ReadOnlyiuserrepository repository; //Constructor Injection PublicTestController (iuserrepository repository) { This. Repository =repository; } [HttpGet] Publichttpresponsemessage Index () {returnMessagetojson.tojson (repository. GetAll ()); } }
Messagetojson just turned the results around. JSON
//need to add System.Web.Extensions.dll reference Public Statichttpresponsemessage ToJson (Object obj) {String str; if(obj isString | | Obj isChar) {STR=obj. ToString (); } Else { varSerializer =NewJavaScriptSerializer (); STR=Serializer. Serialize (obj); } varresult =Newhttpresponsemessage {Content =NewStringcontent (str, encoding.getencoding ("UTF-8"),"Application/json") }; returnresult; }
Finally, test the results:
Visit/api/test/index
Autofac initial use of ASP. NET MVC 3 integrated Web API