Home >Backend Development >C#.Net Tutorial >A brief discussion on AspectCore_Practical skills

A brief discussion on AspectCore_Practical skills

零下一度
零下一度Original
2017-06-15 13:49:502371browse

This article mainly introduces the Asp.Net Core lightweight Aop solution: AspectCore. Friends who need it can refer to 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 modular development concept of Asp.Net Core. Using AspectCore can be more Easily build low-coupling, 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.


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");
    }
   }
 }

Define the ICustomService interface and its implementation class CustomService:


public interface ICustomService
{
  [CustomInterceptor]
  void Call();
}
public class CustomService : ICustomService
{
  public void Call()
  {
    Console.WriteLine("service calling...");
  }
}

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();
  }
}

Register ICustomService, and then configure the container to create a proxy type in ConfigureServices:


public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.AddTransient<ICustomService, CustomService>();
  services.AddMvc();
  services.AddAspectCore();
  return services.BuildAspectCoreServiceProvider();
}

Interceptor configuration. First install the AspectCore.Extensions.Configuration package:


PM> Install-Package AspectCore.Extensions.Configuration

Global interceptor. Use the

overloaded method of AddAspectCore(Actioncf55c8c03f0256412fce639ce72d0b5d), where AspectCoreOptions provides InterceptorFactories to register the global interceptor:


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

Global interceptor with constructor 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");
    }
  }
}

Modify global interceptor registration:


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

As a global interceptor for the service. Add in ConfigureServices:


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

Modify global interceptor registration:


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

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


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

Specific global interception using the

wildcard character Device:


services.AddAspectCore(config =>
{
  config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(PredicateFactory.ForService("*Service"));
});

Provide NonAspectAttribute in AspectCore to prevent Service or Method from being proxied:


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

Supported at the same time The configuration is ignored globally and wildcards are also supported:



 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");
 });

Dependency Injection in the interceptor. 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);
  }
}

Constructor injection needs to make the interceptor as a Service. In addition to the global interceptor, it can still be Use ServiceInterceptor to enable the interceptor to be activated from DI:


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

Service Locator pattern. The interceptor context AspectContext can obtain the current Scoped 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);
  }
}

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

AspectCore provides the RegisterAspectCore extension method to register the services required by the dynamic proxy in the Autofac Container and provide The AsInterfacesProxy and AsClassProxy extension methods enable proxies for interfaces and classes. 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());
}

The above is the detailed content of A brief discussion on AspectCore_Practical skills. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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