欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

什么是AspectCoreProject

這篇文章給大家分享的是有關(guān)什么是AspectCore Project的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比府谷網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式府谷網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋府谷地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

什么是AspectCore Project ?

AspectCore Project 是適用于Asp.Net Core 平臺(tái)的輕量級(jí) Aop(Aspect-oriented programming) 解決方案,它更好的遵循Asp.Net Core的模塊化開發(fā)理念,使用AspectCore可以更容易構(gòu)建低耦合、易擴(kuò)展的Web應(yīng)用程序。AspectCore使用Emit實(shí)現(xiàn)高效的動(dòng)態(tài)代理從而不依賴任何第三方Aop庫。

開使使用AspectCore

啟動(dòng) Visual Studio。從 File 菜單, 選擇 New > Project。選擇 ASP.NET Core Web Application 項(xiàng)目模版,創(chuàng)建新的 ASP.NET Core Web Application 項(xiàng)目。

  • 從 Nuget 安裝 AspectCore.Extensions.DependencyInjection package:

  • PM>   Install-Package AspectCore.Extensions.DependencyInjection

  • 在一般情況下可以使用抽象的InterceptorAttribute自定義特性類,它實(shí)現(xiàn)IInterceptor接口。AspectCore默認(rèn)實(shí)現(xiàn)了基于Attribute的攔截器配置。我們的自定義攔截器看起來像下面這樣:

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

定義ICustomService接口和它的實(shí)現(xiàn)類CustomService:

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

在HomeController中注入ICustomService:

public class HomeController : Controller
{
  private readonly ICustomService _service;
  public HomeController(ICustomService service)
  {
    _service = service;
  }
  public IActionResult Index()
  {
    _service.Call();
    return View();
  }
}

注冊(cè)ICustomService,接著,在ConfigureServices中配置創(chuàng)建代理類型的容器:

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

攔截器配置。首先安裝AspectCore.Extensions.Configuration package:

PM> Install-Package AspectCore.Extensions.Configuration

全局?jǐn)r截器。使用AddAspectCore(Action<AspectCoreOptions>)的重載方法,其中AspectCoreOptions提供InterceptorFactories注冊(cè)全局?jǐn)r截器:

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

帶構(gòu)造器參數(shù)的全局?jǐn)r截器,在CustomInterceptorAttribute中添加帶參數(shù)的構(gòu)造器:

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

修改全局?jǐn)r截器注冊(cè):

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

作為服務(wù)的全局?jǐn)r截器。在ConfigureServices中添加:

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

修改全局?jǐn)r截器注冊(cè):

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

作用于特定Service或Method的全局?jǐn)r截器,下面的代碼演示了作用于帶有Service后綴的類的全局?jǐn)r截器:

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

使用通配符的特定全局?jǐn)r截器:

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

在AspectCore中提供NonAspectAttribute來使得Service或Method不被代理:

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

同時(shí)支持全局忽略配置,亦支持通配符:

 services.AddAspectCore(config =>
 {
   //App1命名空間下的Service不會(huì)被代理
   config.NonAspectOptions.AddNamespace("App1");
   //最后一級(jí)為App1的命名空間下的Service不會(huì)被代理
   config.NonAspectOptions.AddNamespace("*.App1");
   //ICustomService接口不會(huì)被代理
   config.NonAspectOptions.AddService("ICustomService");
   //后綴為Service的接口和類不會(huì)被代理
   config.NonAspectOptions.AddService("*Service");
   //命名為Query的方法不會(huì)被代理
   config.NonAspectOptions.AddMethod("Query");
   //后綴為Query的方法不會(huì)被代理
   config.NonAspectOptions.AddMethod("*Query");
 });

攔截器中的依賴注入。在攔截器中支持屬性注入,構(gòu)造器注入和服務(wù)定位器模式。
屬性注入,在攔截器中擁有public get and set權(quán)限的屬性標(biāo)記[AspectCore.Abstractions.FromServices](區(qū)別于Microsoft.AspNetCore.Mvc.FromServices)特性,即可自動(dòng)注入該屬性,如:

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

構(gòu)造器注入需要使攔截器作為Service,除全局?jǐn)r截器外,仍可使用ServiceInterceptor使攔截器從DI中激活:

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

服務(wù)定位器模式。攔截器上下文AspectContext可以獲取當(dāng)前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);
  }
}

使用Autofac和AspectCore。AspectCore原生支持集成Autofac,我們需要安裝下面兩個(gè)nuget packages:

PM> Install-Package Autofac.Extensions.DependencyInjection
PM> Install-Package AspectCore.Extensions.Autofac

AspectCore提供RegisterAspectCore擴(kuò)展方法在Autofac的Container中注冊(cè)動(dòng)態(tài)代理需要的服務(wù),并提供AsInterfacesProxy和AsClassProxy擴(kuò)展方法啟用interface和class的代理。修改ConfigureServices方法為:

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

感謝各位的閱讀!關(guān)于“什么是AspectCore Project”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

網(wǎng)頁標(biāo)題:什么是AspectCoreProject
轉(zhuǎn)載注明:http://chinadenli.net/article0/jiepio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、靜態(tài)網(wǎng)站、外貿(mào)建站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)