net core 3.1 using autofac and Castle achieve Aop section Programming

The basic construction of the project and the need to introduce a package file

autofac in the use of net core 3.1 and 2.2 have different, so here the record about.

Create a simple demo projects, or api console program and mvc mode can be.

Dependencies successively introduced:

Autofac: providing a container control 
Autofac.Extensions.DependencyInjection: autofac dependency injection of extended 
Autofac.Extras.DynamicProxy: to extend the dynamic proxy autofac 
Castle.Core: using dynamic proxy

  

 

Version inconsistency does not matter, here I loaded the latest package corresponding.

Used in the project

Used in the project there are two places to note:

1. To specify autofac container Program.cs program entry

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())  //指定使用autofac
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

2. Add a container in the startup files Startup entrance ConfigureContainer, the default method and parameters may not be modified

 

 Here I created an interface ISay and implementation Say, is injected into the container.

Code corresponding

public interface ISay
    {
        PeopleEntity SayHello(PeopleEntity peopleEntity);
    }

[Intercept(typeof(InjectInterceptor))]
    public class Say : ISay
    {
        public PeopleEntity SayHello(PeopleEntity peopleEntity)
        {
            peopleEntity.Name = $"hello {peopleEntity.Name}";
            return peopleEntity;
        }
    }

Here we use direct injection in the controller on the line

 

 

 Here's a simple use autofac containers is complete.

Let's use the Castle be implemented using aop

Interceptor creates a new class called InjectInterceptor, and must be achieved IInterceptor (Castle.DynamicProxy in the interface), i.e. virtual Intercept method passes IInvocation interface parameters.

public class InjectInterceptor : IInterceptor
    {
        public virtual void Intercept(IInvocation invocation)
        {
            PreProceed(invocation);
            invocation.Proceed();
            Console.WriteLine(invocation.ReturnValue);
            PostProceed(invocation);
        }

        private void PreProceed(IInvocation invocation)
        {
            Console.WriteLine($"{DateTime.Now} interceptor invoke start");
        }

        private void PostProceed(IInvocation invocation)
        {
            Console.WriteLine($"{DateTime.Now} interceptor invoke end");
        }
    }

 

  在这里,我标记Say的方法,这里指明Intercept只能标记类或接口,不能标记特定的方法,所以你标记对应的类或接口时,内部的方法都会被拦截。

 [Intercept(typeof(InjectInterceptor))]
    public class Say : ISay

  在这里说明,这里是配合autofac使用的所以需要在Startup中的ConfigureContainer方法中注册拦截方法及注册这个Say方法时要开启允许方法拦截。

builder.RegisterType<InjectInterceptor>();
builder.RegisterType<Say>().As<ISay>().EnableInterfaceInterceptors();

  嗯,就这样。

Guess you like

Origin www.cnblogs.com/xianz/p/12315347.html