|
| 1 | +# 1. Getting Started Guide |
| 2 | + |
| 3 | +Version 2.0.0 :arrow_up: (Simple code example: https://github.com/cdcd72/NetCore.AspectCore.AOP.Demo) |
| 4 | +Version 2.0.0 :arrow_down: (Simple code example: https://github.com/fs7744/AspectCoreDemo) |
| 5 | + |
| 6 | +## Getting Started with AspectCore |
| 7 | + |
| 8 | +* Launch Visual Studio. From the File menu, select New > Project. Select the ASP.NET Core Web Application project template and create a new ASP.NET Core Web Application project. |
| 9 | + |
| 10 | +* Install `AspectCore.Extensions.DependencyInjection` package from Nuget: |
| 11 | + ``` |
| 12 | + PM> Install-Package AspectCore.Extensions.DependencyInjection |
| 13 | + ``` |
| 14 | +* Generally, you can customize an attribute class using the abstract `AbstractInterceptorAttribute` which implements the `IInterceptor` interface. AspectCore implements attribute-based interceptor configuration by default. Your custom interceptor looks like this: |
| 15 | + ``` csharp |
| 16 | + public class CustomInterceptorAttribute : AbstractInterceptorAttribute |
| 17 | + { |
| 18 | + public async override Task Invoke(AspectContext context, AspectDelegate next) |
| 19 | + { |
| 20 | + try |
| 21 | + { |
| 22 | + Console.WriteLine("Before service call"); |
| 23 | + await next(context); |
| 24 | + } |
| 25 | + catch (Exception) |
| 26 | + { |
| 27 | + Console.WriteLine("Service threw an exception!"); |
| 28 | + throw; |
| 29 | + } |
| 30 | + finally |
| 31 | + { |
| 32 | + Console.WriteLine("After service call"); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + ``` |
| 37 | +* Define the `ICustomService` interface and its implementation `CustomService`: |
| 38 | + ``` csharp |
| 39 | + public interface ICustomService |
| 40 | + { |
| 41 | + [CustomInterceptor] |
| 42 | + void Call(); |
| 43 | + } |
| 44 | +
|
| 45 | + public class CustomService : ICustomService |
| 46 | + { |
| 47 | + public void Call() |
| 48 | + { |
| 49 | + Console.WriteLine("service calling..."); |
| 50 | + } |
| 51 | + } |
| 52 | + ``` |
| 53 | +* Inject `ICustomService` in `HomeController`: |
| 54 | + ``` csharp |
| 55 | + public class HomeController : Controller |
| 56 | + { |
| 57 | + private readonly ICustomService _service; |
| 58 | + public HomeController(ICustomService service) |
| 59 | + { |
| 60 | + _service = service; |
| 61 | + } |
| 62 | +
|
| 63 | + public IActionResult Index() |
| 64 | + { |
| 65 | + _service.Call(); |
| 66 | + return View(); |
| 67 | + } |
| 68 | + } |
| 69 | + ``` |
| 70 | +* Register `ICustomService`, then configure the container to create proxy types in `ConfigureServices`: |
| 71 | + ``` csharp |
| 72 | + public void ConfigureServices(IServiceCollection services) |
| 73 | + { |
| 74 | + services.AddTransient<ICustomService, CustomService>(); |
| 75 | + services.AddMvc(); |
| 76 | + services.ConfigureDynamicProxy(); |
| 77 | + } |
| 78 | + ``` |
| 79 | +* Finally, add `UseServiceProviderFactory(new DynamicProxyServiceProviderFactory())` in `Program`'s `CreateHostBuilder`: |
| 80 | + ``` csharp |
| 81 | + public static IHostBuilder CreateHostBuilder(string[] args) => |
| 82 | + Host.CreateDefaultBuilder(args) |
| 83 | + .ConfigureWebHostDefaults(webBuilder => |
| 84 | + { |
| 85 | + webBuilder.UseStartup<Startup>(); |
| 86 | + }) |
| 87 | + // ... |
| 88 | + .UseServiceProviderFactory(new DynamicProxyServiceProviderFactory()); |
| 89 | + ``` |
| 90 | +
|
| 91 | +--- |
| 92 | +## Interceptor Configuration |
| 93 | +* Global Interceptors. Use the `ConfigureDynamicProxy(Action<IAspectConfiguration>)` overload method, where `IAspectConfiguration` provides `Interceptors` to register global interceptors: |
| 94 | + ``` csharp |
| 95 | + services.ConfigureDynamicProxy(config => |
| 96 | + { |
| 97 | + config.Interceptors.AddTyped<CustomInterceptorAttribute>(); |
| 98 | + }); |
| 99 | + ``` |
| 100 | + Global interceptor with constructor parameters. Add a parameterized constructor in `CustomInterceptorAttribute`: |
| 101 | + ``` csharp |
| 102 | + public class CustomInterceptorAttribute : AbstractInterceptorAttribute |
| 103 | + { |
| 104 | + private readonly string _name; |
| 105 | + public CustomInterceptorAttribute(string name) |
| 106 | + { |
| 107 | + _name = name; |
| 108 | + } |
| 109 | + public async override Task Invoke(AspectContext context, AspectDelegate next) |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + Console.WriteLine("Before service call"); |
| 114 | + await next(context); |
| 115 | + } |
| 116 | + catch (Exception) |
| 117 | + { |
| 118 | + Console.WriteLine("Service threw an exception!"); |
| 119 | + throw; |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + Console.WriteLine("After service call"); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + ``` |
| 128 | + Modify global interceptor registration: |
| 129 | + ``` csharp |
| 130 | + services.ConfigureDynamicProxy(config => |
| 131 | + { |
| 132 | + config.Interceptors.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" }); |
| 133 | + }); |
| 134 | + ``` |
| 135 | + Global interceptor as a service. Add in `ConfigureServices`: |
| 136 | + ``` csharp |
| 137 | + services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("custom")); |
| 138 | + ``` |
| 139 | + Modify global interceptor registration: |
| 140 | + ``` csharp |
| 141 | + services.ConfigureDynamicProxy(config => |
| 142 | + { |
| 143 | + // Add registered service |
| 144 | + config.Interceptors.AddServiced<CustomInterceptorAttribute>(); |
| 145 | + }); |
| 146 | + ``` |
| 147 | + Global interceptor acting on specific `Service` or `Method`. The following code demonstrates a global interceptor acting on classes with the `Service` suffix: |
| 148 | + ``` csharp |
| 149 | + services.ConfigureDynamicProxy(config => |
| 150 | + { |
| 151 | + config.Interceptors.AddTyped<CustomInterceptorAttribute>(method => method.Name.EndsWith("MethodName")); |
| 152 | + }); |
| 153 | + ``` |
| 154 | + Specific global interceptor using wildcards: |
| 155 | + ``` csharp |
| 156 | + services.ConfigureDynamicProxy(config => |
| 157 | + { |
| 158 | + config.Interceptors.AddTyped<CustomInterceptorAttribute>(Predicates.ForService("*Service")); |
| 159 | + }); |
| 160 | + ``` |
| 161 | +* In AspectCore, `NonAspectAttribute` is provided to prevent `Service` or `Method` from being proxied: |
| 162 | + ``` csharp |
| 163 | + [NonAspect] |
| 164 | + public interface ICustomService |
| 165 | + { |
| 166 | + void Call(); |
| 167 | + } |
| 168 | + ``` |
| 169 | + Also supports global ignore configuration, and wildcards: |
| 170 | + ``` csharp |
| 171 | + services.ConfigureDynamicProxy(config => |
| 172 | + { |
| 173 | + // Services under the App1 namespace will not be proxied |
| 174 | + config.NonAspectPredicates.AddNamespace("App1"); |
| 175 | +
|
| 176 | + // Services under namespaces ending in App1 will not be proxied |
| 177 | + config.NonAspectPredicates.AddNamespace("*.App1"); |
| 178 | +
|
| 179 | + // ICustomService interface will not be proxied |
| 180 | + config.NonAspectPredicates.AddService("ICustomService"); |
| 181 | +
|
| 182 | + // Interfaces and classes with Service suffix will not be proxied |
| 183 | + config.NonAspectPredicates.AddService("*Service"); |
| 184 | +
|
| 185 | + // Methods named Query will not be proxied |
| 186 | + config.NonAspectPredicates.AddMethod("Query"); |
| 187 | +
|
| 188 | + // Methods with Query suffix will not be proxied |
| 189 | + config.NonAspectPredicates.AddMethod("*Query"); |
| 190 | + }); |
| 191 | + ``` |
| 192 | +* Dependency Injection in Interceptors. Supports property injection, constructor injection, and service locator pattern in interceptors. |
| 193 | +
|
| 194 | + Property injection. Mark properties with `public get and set` access in the interceptor with the `[AspectCore.DependencyInjection.FromServiceContextAttribute]` attribute to automatically inject the property, for example: |
| 195 | + ``` csharp |
| 196 | + public class CustomInterceptorAttribute : AbstractInterceptorAttribute |
| 197 | + { |
| 198 | + // Note: Property injection only works when using config.Interceptors.AddTyped<CustomInterceptorAttribute>(); |
| 199 | + // It cannot be used with services.AddSingleton<CustomInterceptorAttribute>() + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced<CustomInterceptorAttribute>(); }); |
| 200 | + [FromServiceContext] |
| 201 | + public ILogger<CustomInterceptorAttribute> Logger { get; set; } |
| 202 | +
|
| 203 | +
|
| 204 | + public override Task Invoke(AspectContext context, AspectDelegate next) |
| 205 | + { |
| 206 | + Logger.LogInformation("call interceptor"); |
| 207 | + return next(context); |
| 208 | + } |
| 209 | + } |
| 210 | + ``` |
| 211 | + Constructor injection requires making the interceptor a `Service`. Besides global interceptors, you can still use `ServiceInterceptor` to activate the interceptor from DI: |
| 212 | +
|
| 213 | + ``` csharp |
| 214 | + public class CustomInterceptorAttribute : AbstractInterceptorAttribute |
| 215 | + { |
| 216 | + private readonly ILogger<CustomInterceptor> ctorlogger; |
| 217 | +
|
| 218 | + // Note: When globally configured with config.Interceptors.AddTyped<CustomInterceptorAttribute>(); constructor injection cannot be automatically injected, needs manual handling |
| 219 | + // Constructor injection only works automatically when using services.AddSingleton<CustomInterceptorAttribute>(); + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced<CustomInterceptorAttribute>(); }); |
| 220 | + public CustomInterceptor(ILogger<CustomInterceptor> ctorlogger) |
| 221 | + { |
| 222 | + this.ctorlogger = ctorlogger; |
| 223 | + } |
| 224 | + } |
| 225 | + ``` |
| 226 | +
|
| 227 | + Service locator pattern. The interceptor context `AspectContext` can get the current scoped `ServiceProvider`: |
| 228 | + ``` csharp |
| 229 | + public class CustomInterceptorAttribute : AbstractInterceptorAttribute |
| 230 | + { |
| 231 | + public override Task Invoke(AspectContext context, AspectDelegate next) |
| 232 | + { |
| 233 | + var logger = context.ServiceProvider.GetService<ILogger<CustomInterceptorAttribute>>(); |
| 234 | + logger.LogInformation("call interceptor"); |
| 235 | + return next(context); |
| 236 | + } |
| 237 | + } |
| 238 | + ``` |
| 239 | +
|
| 240 | +Version 2.0.0 :arrow_up: ps: Original article (https://www.thinkinmd.com/post/2020/03/20/use-aspectcore-to-implement-aop-mechanism/) |
| 241 | +Version 2.0.0 :arrow_down: ps: Original article (http://www.cnblogs.com/liuhaoyang/p/aspectcore-introduction-1.html) |
0 commit comments