Preface:
Sometimes our system needs to open the data to the mobile app side or other mobile devices, have to say that the ASP. WEBAPI is currently the preferred
This section records how the ASP. WEBAPI uses Unity injection. The beginning of the series explains how common ASP. NET MVC uses unity to inject containers
Do not understand what is ioc,di, control reversal of their own Baidu brain, otherwise unable to read this article
In fact, this is also a technical record, because to find the big Baidu incredibly no use of unity injected webapi! Thanks to the friends of the wind reminded. To complete the guidance in this section
For a better understanding, please download the code
Sample code download Https://yunpan.cn/cRTHt5MuKavwH access password 0a47
(This is a common MVC example that already contains the unity injection) contains 4 class libraries that need to be injected, bll,ibll,dal,idal
Development tools: vs2013+sql2012(Database not required) Start: 1. New ASP. NET MVC Webapi Project
2. Install the UNITY.WEBAPI package resolve, there are dependencies: Unity >=4.0.1
(Only unity was installed in our previous injections) so UNITY.WEBAPI needs Unity4.0.1 support.
Installing UNITY.WEBAPI will automatically install many dependencies: Unity (≥4.0.1), Commonservicelocator, Microsoft.aspnet.webapi.core,microsoft.aspnet.webapi.client, Newtonsoft.json
The network is not good will be long, need hard to wait. Or go to nuget official download offline
3.apps.core also needs to be installed Untiy.webapi
After the installation is completed, a file will be automatically Apps.webapi
Add code to UnityConfig.cs
usingApps.core;usingMicrosoft.Practices.Unity;usingSystem.Web.Http;usingUnity.webapi;namespaceapps.webapi{ Public Static classUnityconfig { Public Static voidregistercomponents () {usingunitycontainer.init (); Dependencyregistertype.container_sys (refUsingunitycontainer._container); GlobalConfiguration.Configuration.DependencyResolver=NewUnity.WebApi.UnityDependencyResolver (Usingunitycontainer._container); } }}
last added unityconfig.registercomponents (); to Global.asax. Must be added in the global file, this injection is run-time injected
usingApps.core;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;namespaceapps.webapi{ Public classWebApiApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Globalconfiguration.configure (Webapiconfig.register); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); Unityconfig.registercomponents (); } }}
4. Fix Errors
Error occurs after running
This is caused by a lack of parsing of the Web. config node.
You need to add the following nodes
<dependentassembly> <assemblyidentityname= "System.Web.Http"PublicKeyToken= "31bf3856ad364e35" /> <BindingRedirectoldversion= "1.0.0.0-5.2.3.0"newversion= "5.2.3.0" /> </dependentassembly> <dependentassembly> <assemblyidentityname= "System.Net.Http.Formatting"PublicKeyToken= "31bf3856ad364e35" /> <BindingRedirectoldversion= "1.0.0.0-5.2.3.0"newversion= "5.2.3.0" /></dependentassembly>
After the run, there is no problem.
Write a simple request test in home/index.cshtml
<Scriptsrc= "~/scripts/jquery-1.10.2.min.js"></Script><Script>$(function() {$.get ("/API/VALUES/5",function(data) {alert (data); }); });</Script>
5. Run and debug
Add control reversal code to Valuescontroller
[Dependency] Public ISYSPERSONBLL M_BLL {get; set;}
So that we can use the M_BLL to access the BLL method, to achieve the purpose of control reversal
If the breakpoint executes to the call, then congratulations, the injection is successful, can be called ( with the image to return the first data of the database ID, data and script in the source download view )
can access the database smoothly
Last reference code download https://yunpan.cn/cBF9tkaE8ayy7 access password f60b
Build a back-end management System (MVC5+EF6+EASYUI)-WEBAPI and Unity Injection with ASP. 1.4.3+unity4.x Injection