NET Core integration Autofac and Castle
Read Catalogue
- Objective:
- AUTOFAC in 1.asp.net Core
- 2. Integrating Castle's Dynamicproxy
- 3. Precautions
Back to catalogue preface:
In addition to the IOC container Asp.netcore comes with, we can also use other mature di frameworks, such as Autofac,structuremap (I've only used Unity,ninject and Castle).
Back to directory 1. AUTOFAC in ASP. NET Core
First, add the following reference to the dependency node for Project.json:
- " Microsoft.Extensions.DependencyInjection": "1.0.0",
- " Autofac": "4.1.1",
- " Autofac.Extensions.DependencyInjection": "4.0.0",
Then we also modify the Configureservices method in the startup file, in order to take over the default Di, we want to add the return value Autofacserviceprovider for the function;
1.1 Configureservices function
- Public IServiceProvider configureservices(iservicecollection services)
- {
- Services. Addapplicationinsightstelemetry(Configuration);
- Services. Addmvc();
- return registerautofac(services);
- }
1.2 Registerautofac function
- Private IServiceProvider registerautofac(iservicecollection Services)
- {
- var builder = new containerbuilder();
- Builder. Populate(services);
- var assembly = this . GetType(). GetTypeInfo(). Assembly;
- Builder. Registertype<aopinterceptor>();
- Builder. Registerassemblytypes(Assembly)
- . Where(type =
- typeof(idependency). IsAssignableFrom(type) && ! Type. GetTypeInfo(). IsAbstract)
- . asimplementedinterfaces()
- . instanceperlifetimescope(). Enableinterfaceinterceptors(). Interceptedby(typeof(aopinterceptor));
- this. Applicationcontainer = builder. Build();
- return new autofacserviceprovider(this. Applicationcontainer);
- }
Here Idependency interface is an empty interface, in order to scan to implement this interface class, automatic completion of injection operations.
Back to Table of contents 2. Integration of Castle Dynamicproxy
To achieve consolidation, you only need the above function, this code:
- . enableinterfaceinterceptors(). Interceptedby(typeof(aopinterceptor));
2.2 Referencing assemblies
Obviously some assemblies do not have the corresponding version of the core Autofac.Extras.DynamicProxy, since the good to integrate, modify the source code it.
Autofac.Extras.DynamicProxy's inability to support the core is mainly due to the lack of new reflection api,gettypeinfo in the source code or the use of some remoting APIs.
Support for core Autofac.Extras.DynamicProxy source code content and demo's GitHub address are as follows:
Https://github.com/maxzhang1985/AutofacCastle.AspNetCore.Demo
Back to Table of contents 3. Precautions
(1). When creating a AUTOFAC container, do not forget to modify the return value of Configureservices to IServiceProvider.
(2). For the different life cycles mentioned by the ASP. AUTOFAC also defines the corresponding extension method, such as Instanceperlifetimescope, which defaults to transient mode, This model is also included in the context of entityframwork.
(3). AUTOFAC core does not support injection from view, but can be used in conjunction with ASP. NET core's own IOC container.
GITHUB:HTTPS://GITHUB.COM/MAXZHANG1985/YOYOFX If you feel you can also invite the Star , Welcome to communicate together.
Communication groups for. NET Core and YOYOFX: 214741894
If you think this article is helpful to you, please click " recommend ", thank you.
- If you feel that reading this article is helpful to you, please click " recommend " button, your "recommendation" will be my biggest writing motivation! Welcome to reprint, but without the author's consent, reprint article must be in the article page obvious location to the author and the original link , otherwise reserve the right to pursue legal responsibility.
Category:. NET Core
NET Core integration Autofac and Castle