What is the AspectCore Project?

零下一度
Release: 2017-06-24 10:17:00
Original
1732 people have browsed it

What is AspectCore Project?

AspectCore Project is a lightweight Aop (Aspect-oriented programming) solution suitable for the Asp.Net Core platform. It better follows the modularization of Asp.Net Core Development concept, using AspectCore makes it easier to build low-coupling and easily scalable web applications. AspectCore uses Emit to implement efficient dynamic proxy without relying on any third-party Aop library.

Start using AspectCore

  • Start Visual Studio. From the File menu, choose New > Project. Select the ASP.NET Core Web Application project template and create a new ASP.NET Core Web Application project.

  • Install from NugetAspectCore.Extensions.DependencyInjection package:

    PM>   Install-Package AspectCore.Extensions.DependencyInjection
    Copy after login
  • In general, you can use the abstractInterceptorAttribute Custom attribute class, which implements the IInterceptor interface. AspectCore implements interceptor configuration based on Attribute by default. Our custom interceptor looks like this:

    public class CustomInterceptorAttribute : InterceptorAttribute
    {
        public async override Task Invoke(IAspectContext context, AspectDelegate next)
        {
            try
            {
                Console.WriteLine("Before service call");
                await next(context);
            }
            catch (Exception)
            {
                Console.WriteLine("Service threw an exception!");
                throw;
            }
            finally
            {
                Console.WriteLine("After service call");
            }
         }
     }
    Copy after login
  • DefinitionICustomServiceinterface and its implementation classCustomService:

    public interface ICustomService
    {
        [CustomInterceptor]
        void Call();
    }
    
    public class CustomService : ICustomService
    {
        public void Call()
        {
            Console.WriteLine("service calling...");
        }
    }
    Copy after login
  • Inject ICustomService in HomeController:

    public class HomeController : Controller
    {
        private readonly ICustomService _service;
        public HomeController(ICustomService service)
        {
            _service = service;
        }
    
        public IActionResult Index()
        {
            _service.Call();
            return View();
        }
    }
    Copy after login
  • RegisterICustomService, Next, configure the container to create the proxy type in ConfigureServices:

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<ICustomService, CustomService>();
        services.AddMvc();
        services.AddAspectCore();
        return services.BuildAspectCoreServiceProvider();
    }
    Copy after login
  • Interceptor configuration. First install the AspectCore.Extensions.Configuration package:

    PM> Install-Package AspectCore.Extensions.Configuration
    Copy after login

    global interceptor. Use the overloaded method of AddAspectCore(Action<AspectCoreOptions>), where AspectCoreOptions provides InterceptorFactories to register a global interceptor:

     services.AddAspectCore(config =>
     {
          config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>();
     });
    Copy after login

    with constructor For the global interceptor of parameters, add a constructor with parameters in CustomInterceptorAttribute:

    public class CustomInterceptorAttribute : InterceptorAttribute
    {
        private readonly string _name;
        public CustomInterceptorAttribute(string name)
        {
            _name = name;
        }
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            try
            {
                Console.WriteLine("Before service call");
                await next(context);
            }
            catch (Exception)
            {
                Console.WriteLine("Service threw an exception!");
                throw;
            }
            finally
            {
                Console.WriteLine("After service call");
            }
        }
    }
    Copy after login

    Modify the global interceptor registration:

    services.AddAspectCore(config =>
    {
         config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" });
    });
    Copy after login

    as the global interceptor of the service. Add in ConfigureServices:

    services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("service"));
    Copy after login

    Modify the global interceptor registration:

    services.AddAspectCore(config =>
    {
        config.InterceptorFactories.AddServiced<CustomInterceptorAttribute>();
    });
    Copy after login

    Acts on a specific Service or Method Global interceptor, the following code demonstrates a global interceptor acting on a class with the Service suffix:

    services.AddAspectCore(config =>
    {
        config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service"));
    });
    Copy after login

    Specific global interceptor using wildcards:

    services.AddAspectCore(config =>
    {
        config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(PredicateFactory.ForService("*Service"));
    });
    Copy after login
  • Provide NonAspectAttribute in AspectCore to prevent Service or Method from being proxied:

    [NonAspect]
    public interface ICustomService
    {
        void Call();
    }
    Copy after login

    also supports global ignore configuration , also supports wildcards:

     services.AddAspectCore(config =>
      {
          //App1命名空间下的Service不会被代理
          config.NonAspectOptions.AddNamespace("App1");
    
          //最后一级为App1的命名空间下的Service不会被代理
          config.NonAspectOptions.AddNamespace("*.App1");
    
          //ICustomService接口不会被代理
          config.NonAspectOptions.AddService("ICustomService");
    
          //后缀为Service的接口和类不会被代理
          config.NonAspectOptions.AddService("*Service");
    
          //命名为Query的方法不会被代理
          config.NonAspectOptions.AddMethod("Query");
    
          //后缀为Query的方法不会被代理
          config.NonAspectOptions.AddMethod("*Query");
      });
    Copy after login
  • Dependency injection in interceptors. Supports property injection, constructor injection and service locator patterns in interceptors.
    Property injection, property tags with public get and set permissions in the interceptor [AspectCore.Abstractions.FromServices](different from Microsoft.AspNetCore.Mvc. FromServices) feature, you can automatically inject this property, such as:

    public class CustomInterceptorAttribute : InterceptorAttribute
    {
        [AspectCore.Abstractions.FromServices]
        public ILogger<CustomInterceptorAttribute> Logger { get; set; }
    
    
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            Logger.LogInformation("call interceptor");
            return next(context);
        }
    }
    Copy after login

    Constructor injection needs to make the interceptor as Service, in addition to the global interceptor, you can still use ServiceInterceptor Make the interceptor active from DI:

    public interface ICustomService
    {
        [ServiceInterceptor(typeof(CustomInterceptorAttribute))]
        void Call();
    }
    Copy after login

    Service locator mode. The interceptor context AspectContext can obtain the current Scoped's ServiceProvider:

    public class CustomInterceptorAttribute : InterceptorAttribute
    {
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var logger = context.ServiceProvider.GetService<ILogger<CustomInterceptorAttribute>>();
            logger.LogInformation("call interceptor");
            return next(context);
        }
    }
    Copy after login
  • using Autofac and AspectCore. AspectCore natively supports integrating Autofac. We need to install the following two nuget packages:

    PM> Install-Package Autofac.Extensions.DependencyInjection
    PM> Install-Package AspectCore.Extensions.Autofac
    Copy after login

    AspectCore provides the RegisterAspectCore extension method to register the services required by the dynamic proxy in Autofac's Container , and provide AsInterfacesProxy and AsClassProxy extension methods to enable the proxy of interface and class. Modify the ConfigureServices method to:

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    
        var container = new ContainerBuilder();
    
        container.RegisterAspectCore();
    
        container.Populate(services);
    
        container.RegisterType<CustomService>().As<ICustomService>().InstancePerDependency().AsInterfacesProxy();
    
        return new AutofacServiceProvider(container.Build());
    }
    Copy after login

Feedback on issues

If you have any questions, please submit an Issue to us.
AspectCore Project project address:

Finally. . .

Looking for a job, welcome to recommend .NET/.NET Core back-end development positions, located in Shanghai, please send a private message or email.

The above is the detailed content of What is the AspectCore Project?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!