<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        Asp.Net Core輕量級(jí)Aop解決方案:AspectCore

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:35:41
        文檔

        Asp.Net Core輕量級(jí)Aop解決方案:AspectCore

        Asp.Net Core輕量級(jí)Aop解決方案:AspectCore:什么是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)用
        推薦度:
        導(dǎo)讀Asp.Net Core輕量級(jí)Aop解決方案:AspectCore:什么是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 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)目。

      1. 從 Nuget 安裝 AspectCore.Extensions.DependencyInjection package:
      2. PM>   Install-Package AspectCore.Extensions.DependencyInjection
      3. 在一般情況下可以使用抽象的InterceptorAttribute自定義特性類,它實(shí)現(xiàn)IInterceptor接口。AspectCore默認(rèn)實(shí)現(xiàn)了基于Attribute的攔截器配置。我們的自定義攔截器看起來像下面這樣:
      4. 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());
        }

        有問題反饋

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

        AspectCore Project 項(xiàng)目地址: https://github.com/aspectcore

        以上所述是小編給大家介紹的Asp.Net Core輕量級(jí)Aop解決方案:AspectCore,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Asp.Net Core輕量級(jí)Aop解決方案:AspectCore

        Asp.Net Core輕量級(jí)Aop解決方案:AspectCore:什么是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)用
        推薦度:
        標(biāo)簽: 解決方案 core 輕量級(jí)
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 免费人成视频在线观看不卡| 好吊妞在线成人免费| 中文字幕专区在线亚洲| 美女露100%胸无遮挡免费观看| 免费毛片在线看片免费丝瓜视频| 亚洲国产片在线观看| **毛片免费观看久久精品| 亚洲国产成人精品无码一区二区| 13一14周岁毛片免费| 亚洲最大成人网色香蕉| 好男人视频社区精品免费| 亚洲中文字幕久久精品无码VA| 四虎成人免费大片在线| 青娱乐在线视频免费观看| 亚洲视频在线一区二区| a级在线免费观看| 亚洲成a人片在线观| 免费观看一级毛片| 三级片免费观看久久| 精品国产亚洲一区二区三区| 日韩免费人妻AV无码专区蜜桃| 亚洲成人福利在线| 又黄又爽的视频免费看| 久久九九全国免费| 亚洲人成小说网站色| 亚洲国产婷婷香蕉久久久久久| 无码精品人妻一区二区三区免费看 | 亚洲国产综合人成综合网站00| 国产免费不卡v片在线观看 | 国产精品亚洲а∨无码播放不卡| 亚洲阿v天堂在线2017免费| 中文字幕在线免费播放| 亚洲精品免费观看| 日韩毛片无码永久免费看| 美女网站在线观看视频免费的| 亚洲视频手机在线| 国产免费69成人精品视频| 免费h视频在线观看| 亚洲综合小说另类图片动图| 久久亚洲高清综合| 欧美大尺寸SUV免费|