激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - Asp.Net Core輕量級Aop解決方案:AspectCore

Asp.Net Core輕量級Aop解決方案:AspectCore

2020-05-06 13:46時光何以染塵埃 ASP.NET教程

這篇文章主要介紹了Asp.Net Core輕量級Aop解決方案:AspectCore,需要的朋友可以參考下

什么是AspectCore Project ?

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

開使使用AspectCore

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

  • 從 Nuget 安裝 AspectCore.Extensions.DependencyInjection package:
  • PM>   Install-Package AspectCore.Extensions.DependencyInjection
  • 在一般情況下可以使用抽象的InterceptorAttribute自定義特性類,它實現IInterceptor接口。AspectCore默認實現了基于Attribute的攔截器配置。我們的自定義攔截器看起來像下面這樣:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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接口和它的實現類CustomService:

?
1
2
3
4
5
6
7
8
9
10
11
12
public interface ICustomService
{
  [CustomInterceptor]
  void Call();
}
public class CustomService : ICustomService
{
  public void Call()
  {
    Console.WriteLine("service calling...");
  }
}

在HomeController中注入ICustomService:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class HomeController : Controller
{
  private readonly ICustomService _service;
  public HomeController(ICustomService service)
  {
    _service = service;
  }
  public IActionResult Index()
  {
    _service.Call();
    return View();
  }
}

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

?
1
2
3
4
5
6
7
public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.AddTransient<ICustomService, CustomService>();
  services.AddMvc();
  services.AddAspectCore();
  return services.BuildAspectCoreServiceProvider();
}

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

?
1
PM> Install-Package AspectCore.Extensions.Configuration

全局攔截器。使用AddAspectCore(Action<AspectCoreOptions>)的重載方法,其中AspectCoreOptions提供InterceptorFactories注冊全局攔截器:

?
1
2
3
4
services.AddAspectCore(config =>
{
  config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>();
});

帶構造器參數的全局攔截器,在CustomInterceptorAttribute中添加帶參數的構造器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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");
    }
  }
}

修改全局攔截器注冊:

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

作為服務的全局攔截器。在ConfigureServices中添加:

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

修改全局攔截器注冊:

?
1
2
3
4
services.AddAspectCore(config =>
{
  config.InterceptorFactories.AddServiced<CustomInterceptorAttribute>();
});

作用于特定Service或Method的全局攔截器,下面的代碼演示了作用于帶有Service后綴的類的全局攔截器:

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

使用通配符的特定全局攔截器:

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

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

?
1
2
3
4
5
[NonAspect]
public interface ICustomService
{
  void Call();
}

同時支持全局忽略配置,亦支持通配符:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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");
});

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

?
1
2
3
4
5
6
7
8
9
10
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);
  }
}

構造器注入需要使攔截器作為Service,除全局攔截器外,仍可使用ServiceInterceptor使攔截器從DI中激活:

?
1
2
3
4
5
public interface ICustomService
{
  [ServiceInterceptor(typeof(CustomInterceptorAttribute))]
  void Call();
}

服務定位器模式。攔截器上下文AspectContext可以獲取當前Scoped的ServiceProvider:

?
1
2
3
4
5
6
7
8
9
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,我們需要安裝下面兩個nuget packages:

?
1
2
PM> Install-Package Autofac.Extensions.DependencyInjection
PM> Install-Package AspectCore.Extensions.Autofac

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

?
1
2
3
4
5
6
7
8
9
10
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());
}

有問題反饋

如果您有任何問題,請?zhí)峤?Issue 給我們。

AspectCore Project 項目地址: https://github.com/aspectcore

以上所述是小編給大家介紹的Asp.Net Core輕量級Aop解決方案:AspectCore,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.cnblogs.com/liuhaoyang/p/aspectcore-introduction-1.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品久久久久www | 国产亚洲精品久久午夜玫瑰园 | 国产精品性夜天天视频 | 蜜桃精品视频 | h视频免费观看 | 中文字幕xxx | av在线等 | 久久精品国产99国产精品亚洲 | 成人国产精品久久 | 日日操夜夜操狠狠操 | jizzjizz中国少妇中文 | 啪啪毛片 | 亚洲视频观看 | 中国洗澡偷拍在线播放 | 成年人免费视频大全 | 久久国产经典 | 91国内精品久久久久免费影院 | 中文字幕免费看 | 在线中文字幕不卡 | 叉逼视频 | 久久久精品网站 | 日韩精品久久久 | 91久久国产综合精品女同国语 | 欧美日韩视频在线播放 | 久久精品国产一区二区电影 | 青青国产在线视频 | 一边吃奶一边摸下娇喘 | 欧美成人免费电影 | 天堂在线中文资源 | 免费中文视频 | 成人免费自拍视频 | 成人18免费观看 | 免费黄色一级 | 免费观看国产视频 | 九九视频久久 | 中午字幕无线码一区2020 | 国产三级午夜理伦三级 | 免费观看黄色一级视频 | 精品一区二区在线观看视频 | 免费放黄网站在线播放 | 我爱我色成人网 |