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{public interface Itestservice { string GetData (); String Getmaindata ();} }
Create a new Testservice class implementation itestservice with the following code:
namespace domainservices{public class Testservice:itestservice {public string GetData () { Return "This is the services data acquired by the plugin"; } public string Getmaindata () { Return "This is the services data acquired by the main project";} }
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{public class Autofacbootstrapper {public static void Autofacinit () {var builder = new Containerbuilder (); Registered domainservices var services = assembly.load ("domainservices"); Builder. Registerassemblytypes (services, services). Where (t = = T.name.endswith ("Service")). Asimplementedinterfaces (). Propertiesautowired (); Implement plug-in controllers injection var assemblies = new DirectoryInfo (HttpContext.Current.Server.MapPath ( "~/app_data/plugins/")). GetFiles ("*.dll"). Select (r = Assembly.LoadFrom (R.fullname)). ToArray (); foreach (var assembly in assemblies) {Builder. Registercontrollers (assembly). Propertiesautowired (); }//Register the master project's ControLlers 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{Public class MvcApplication:System.Web.HttpApplication { protected void Application_Start () { Arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); Enable AUTOFAC injection autofacbootstrapper.autofacinit ();}}}
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{public class 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";} <div class= "Jumbotron" >
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{public 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> <P>AUTOFAC injection plugin: @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