NET Core integration Autofac and Castle

Source: Internet
Author: User

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:

    1. " Microsoft.Extensions.DependencyInjection": "1.0.0",
    2. " Autofac": "4.1.1",
    3. " 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
  1. Public IServiceProvider configureservices(iservicecollection services)
  2. {
  3. Services. Addapplicationinsightstelemetry(Configuration);
  4. Services. Addmvc();
  5. return registerautofac(services);
  6. }
1.2 Registerautofac function
  1. Private IServiceProvider registerautofac(iservicecollection Services)
  2. {
  3. var builder = new containerbuilder();
  4. Builder. Populate(services);
  5. var assembly = this . GetType(). GetTypeInfo(). Assembly;
  6. Builder. Registertype<aopinterceptor>();
  7. Builder. Registerassemblytypes(Assembly)
  8. . Where(type =
  9. typeof(idependency). IsAssignableFrom(type) && ! Type. GetTypeInfo(). IsAbstract)
  10. . asimplementedinterfaces()
  11. . instanceperlifetimescope(). Enableinterfaceinterceptors(). Interceptedby(typeof(aopinterceptor));
  12. this. Applicationcontainer = builder. Build();
  13. return new autofacserviceprovider(this. Applicationcontainer);
  14. }

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:

    1. . 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.


    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.