This article is a mirror article of machine translation, please click here to jump to the original article.

View: 27262|Reply: 4

[Source] Use autofac in MVC to implement automatic registration

[Copy link]
Posted on 11/20/2019 9:44:39 AM | | |
The idea is simple: first we find all the DLLs, then we find the class that implements the IAutoInject interface, and then use RegisterAssemblyTypes to inject it. IAutoInject is an empty interface that assists us in finding interfaces that need to be registered

1. Run the command in the console to install the AutoFac component



2. Define the IAutoInject interface



3. Initialize in Application_Start().
Get all assemblies for a project method:

.Net Core gets all assemblies of the project, excluding Microsoft and Nuget packages
https://www.itsvse.com/thread-8994-1-1.html
(Source: Architect_Programmer)



Note: Get the wrong spelling of the assembly (which causes the registration information to be lost after the program pool is reclaimed):var assemblies = AppDomain.CurrentDomain.GetAssemblies(). ToArray();


4. Write test code

Visit Home/index to see the OK output successfully





Previous:Parsing convolutional neural networks
Next:.Net Core gets all assemblies of the project, excluding Microsoft and Nuget packages
 Landlord| Posted on 11/20/2019 10:38:53 AM |
If the interface is generic, the judgment is as follows:

 Landlord| Posted on 11/20/2019 10:39:23 AM |
Determine whether an attribute is a set generic

 Landlord| Posted on 11/20/2019 10:46:42 AM |
Posted on 1/21/2022 10:48:45 AM |
Support support support support
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com