has been doing maintenance projects, without the opportunity to contact the complete architecture, so self-taught. NET MVC. Since today, the process of self-study, problems encountered and solutions have been recorded.
Right-click in the WebApp project and use NuGet to introduce Unity3.0.
A Bootstrapper.cs file is automatically generated when the project directory is introduced.
Manually create the Ioc.config file under the WebApp root directory to configure IOC rules. (Note: The configuration file in the online tutorial before the,<typealiases></typealiases> node and the <unity></unity> node peer, but the runtime is always abnormal, The definition of ibaseservice could not be found. The assembly that contains the custom type should not be found. After I move the <typeAliases></typeAliases> node to the inside of <unity></unity>, the exception is gone. )
1 <?xml version="1.0"?>2 <configuration>3 <configSections>4 <section Name="Unity"Type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,5 Microsoft.Practices.Unity.Configuration"/>6 </configSections>78 <unity>9 <typeAliases><!--life cycle type aliases--<typealias alias="Singleton"Type="Microsoft.practices.unity.containercontrolledlifetimemanager,microsoft.practices.unity"/><typealias alias="External"Type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/><!--user-defined type aliases--<typealias alias="Ibaseservice"Type="Framework.Services.IBaseService, Framework.services"/><typealias alias="Baseservice"Type="Framework.Services.BaseService, Framework.services"/></typeAliases><containers><container><types><!--default is the full lifecycle, which is a singleton mode--21 <type type= " Ibaseservice "Mapto=" Baseservice "/>22 </container>23 </containers>24 </unity>25 < Startup>26 <supportedruntime version= "v4.0 "Sku=" "/>27 </startup>< Span style= "color: #008080;" >28 </configuration>
Modify the Buildunitycontainer () method in the Bootstrapper.cs file:
1PrivateStaticIunitycontainer Buildunitycontainer ()2{3var container =NewUnityContainer ();45//Register all your components and the container here6//It isn't necessary to register your controllers78//Get the IOC configuration file location9String configfile = System.Web.HttpRuntime.AppDomainAppPath +"Ioc.config";10//Setting the extension profile type11var filemap =New Execonfigurationfilemap {execonfigfilename =ConfigFile};12//Reading configuration information from the config fileSystem.Configuration.Configuration config =14 Configurationmanager.openmappedexeconfiguration (Filemap, Configurationuserlevel.none); 15 // Gets the configuration section for the specified name 16 unityconfigurationsection configuration = (unityconfigurationsection) config. GetSection ( "unity" Span style= "color: #000000;" >); 17 // Configure IOC container 18 configuration. Configure (container); 19 20 return container; 21}
Initialize the IOC container in Globale.asax.cs:
1ProtectedvoidApplication_Start ()2{3 Arearegistration.registerallareas (); 5 Filterconfig.registerglobalfilters ( Globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Span style= "color: #008080;" > 8 Bundleconfig.registerbundles (bundletable.bundles); Authconfig.registerauth (); 11 bootstrapper.initialise (); // Initialize IOC container 12}
Add the Ibaseservice type attribute to the controller, plus the [Dependency] tag so that it can be used directly in the controller.
The IOC Framework Unity 3.0 was introduced to completion.