I. Preface
Because of the complexity of the project business, multiple plugins were created and placed in different projects, the project uses the IOC of AUTOFAC, but the main project can be injected, the plug-in injection fails,
There are no parameterless constructors defined for this object. Here's a step-by-step injection of the plug-in project.
Two. Create a new project with plug-ins
Reference: Simple implementation of the ASP. NET MVC5 plugin mechanism
Project structure such as:
Three. Create a Domainservices class library
Create a new Itestservice interface with the following code:
namespace domainservices{ interface itestservice { string GetData (); string Getmaindata ();} }
Create a new Testservice class implementation itestservice with the following code:
Namespace domainservices{Publicclass testservice:itestservice { public string GetData () Span class= "indent" > { return " This is the services data acquired by the plugin"; } public string Getmaindata () { return "}}} /span>
Four. AUTOFAC implementation of master project injection and plug-in injection
1. Main project references AUTOFAC, AUTOFAC. Integration.mvc
Tools, library Package Manager, management solution NuGet Package:
2. Create a new AutoFacBootStrapper.cs class for the main project to implement AUTOFAC injection
The code is as follows:
Using System.IO;Using System.Linq;Using System.Reflection;Using System.Web;Using SYSTEM.WEB.MVC;Using AUTOFAC;Using AUTOFAC.INTEGRATION.MVC;Namespace web{PublicClass Autofacbootstrapper {PublicStaticvoid Autofacinit () { var builder =New Containerbuilder (); //Register Domainservices var services = Assembly.Load ("Domainservices ");Builder. Registerassemblytypes (services, services). Where (t = t.name.endswith ("Service ")). Asimplementedinterfaces (). Propertiesautowired (); //Implementing plug-in controllers injection var assemblies =New DirectoryInfo (HttpContext.Current.Server.MapPath ("~/app_data/plugins/")) "*.dll") foreach (var assembly in assemblies) { Builder. Registercontrollers (assembly). Propertiesautowired (); } // Register the controllers< of the main project Span class= "indent" > Builder. Registercontrollers (assembly.getexecutingassembly ()). Propertiesautowired (); var container = Builder. Build (); Dependencyresolver.setresolver (new autofacdependencyresolver ( container)); } }}
3. Enable AUTOFAC injection, add autofacbootstrapper.autofacinit () to the global program start location;
Using system.web.mvc;using system.web.optimization;using system.web.routing;namespace Web{PublicClass MvcApplication:System.Web.HttpApplication{ protected void Application_Start () { Arearegistration.registerallarea S (); Filterconfig.registerglobalfilters ( Globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Span class= "indent" > Bundleconfig.registerbundles (bundletable.bundles) ; //enable AUTOFAC injection Autofacbootstrapper.autofacinit (); }}} /span>
Five. Test the success of AUTOFAC injection
1. HomeController of the main web gives the constructor injection demo
Using system.web.mvc;using domainservices;namespace web.controllers{PublicClass Homecontroller:controller{ //public itestservice Service {get; set;} Itestservice _service; public HomeController (Itestservice service) { _service = Service; } public ActionResult Index () { Viewbag.show = _service. Getmaindata (); return View (); }}}
View code for the main project:
@{Viewbag.title = "Home page";}<Divclass="Jumbotron "> <H1>asp.net</H1> <Pclass="The lead ">asp.net are a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</P> <P><a href= " http://asp.net "class=" btn Btn-primary btn-large ">learn more»</a></< Span class= "title" >p></div>< div class= "row" > <p> here is the main project of AUTOFAC injection: @ViewBag. Show </ p> @Html. ActionLink ("Demo plugin content access Click here", "Index", "Home", new {area = "Demo" }, NULL) </DIV>
2. Plugin Homecontrollers gives a property injection demo
Note: The AUTOFAC website does not recommend the use of attribute injection, in order to facilitate the demonstration, I also added the Autofacbootstrapper attribute injection. The time of the specific project is recommended using the method of constructor injection.
Using system.web.mvc;using domainservices;namespace plugin.demo.controllers{ class Homecontroller:controller {public itestservice Service {get; set;} Public ActionResult Index () { viewbag.show=service.getdata (); return View (); } }}
Plugin's View Code:
<div> <p>asp.net MVC plugin: plugin.demo content </<p> AUTOFAC Injection Plug-in: @ViewBag. Show</p></div>
3. Regenerate the plugin and run the main project with the following effects:
After the main project page AUTOFAC injection succeeds, the domainservices data is called as follows:
Plug - ins:
Simple implementation of plug-ins in Autofac Dependency injection ASP MVC5 plug-in mechanism