### Install AspectCore.Extension.Reflection Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/reflection-extensions.md Install the AspectCore.Extension.Reflection package using the NuGet Package Manager Console. ```powershell Install-Package AspectCore.Extensions.Reflection -pre ``` -------------------------------- ### Install AspectCore.Core Package Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md Install the AspectCore.Core package via NuGet to get started with AspectCore.DependencyInjection. ```powershell Install-Package AspectCore.Core -pre ``` -------------------------------- ### Dependency Injection Example Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/injector.en.md Illustrates constructor injection and property injection with the FromServiceContext attribute. ```csharp public interface ISampleService { } public class SampleService : ISampleService { private readonly ISampleRepository _sampleRepository; private readonly ILogger _logger; // Constructor injection public SampleService(ISampleRepository sampleRepository, ILogger logger) { _sampleRepository = sampleRepository; _logger = logger; } } public interface ISampleRepository { } public class SampleRepository : ISampleRepository { // Property injection. Property injection conditions: marked with FromServiceContext attribute and allows set. Properties meeting conditions are automatically injected [FromServiceContext] public ILogger Logger { get; set; } } ``` -------------------------------- ### Install AspectCore NuGet Package Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Install the AspectCore.Extensions.DependencyInjection package using the Package Manager Console. ```powershell PM> Install-Package AspectCore.Extensions.DependencyInjection ``` -------------------------------- ### Install AspectCore.Extensions.DependencyInjection Package Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md Install the AspectCore.Extensions.DependencyInjection NuGet package for integrating AspectCore.Injector with ASP.NET Core. ```powershell Install-Package AspectCore.Extensions.DependencyInjection -pre ``` -------------------------------- ### Custom Interceptor Implementation Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Example implementation of a custom interceptor attribute with constructor and invocation logic. ```csharp public class CustomInterceptorAttribute : AbstractInterceptorAttribute { 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"); } } } ``` -------------------------------- ### Register Global Interceptor Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Register a global interceptor using the ConfigureDynamicProxy method. This example adds a typed interceptor. ```csharp services.ConfigureDynamicProxy(config => { config.Interceptors.AddTyped(); }); ``` -------------------------------- ### Custom Interceptor Constructor Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Example of a custom interceptor constructor. Note the limitation regarding constructor injection when globally configured with `AddTyped`. ```csharp // Note: When globally configured with config.Interceptors.AddTyped(); constructor injection cannot be automatically injected, needs manual handling // Constructor injection only works automatically when using services.AddSingleton(); + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced(); }); public CustomInterceptor(ILogger ctorlogger) { this.ctorlogger = ctorlogger; } } ``` -------------------------------- ### Method Definition with Attributes Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/reflection-extensions.en.md Defines a method with various custom attributes applied to it. This serves as the target for attribute retrieval examples. ```csharp [Attribute1] [Attribute2("benchmark", Id = 10000)] [Attribute3] [Attribute3] [Attribute3] public void Method() { } ``` -------------------------------- ### Custom Interceptor Constructor with Logger Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/1.Getting-Started.md Example of a custom interceptor's constructor that accepts an ILogger for dependency injection. Note the comment regarding limitations with global configuration. ```csharp // ps: When globally configured with config.Interceptors.AddTyped(), constructor injection cannot be automatically injected, needs manual handling // Only works with services.AddSingleton(); + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced(); }); public CustomInterceptor(ILogger ctorlogger) { this.ctorlogger = ctorlogger; } ``` -------------------------------- ### Retrieve Attributes Using MethodReflector Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/reflection-extensions.md Demonstrates how to get custom attributes from a method using the MethodReflector. This approach offers a performance advantage over standard reflection. ```csharp var method = type.GetMethod("Method"); var reflector = method.GetReflector(); var attribute1 = reflector.GetCustomAttribute(typeof(Attribute1)); var attributes = reflector.GetCustomAttributes(); ``` -------------------------------- ### Custom Interceptor Implementation Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Implement a custom interceptor by inheriting from AbstractInterceptorAttribute and overriding the Invoke method. This example demonstrates logging before and after a service call, and handling exceptions. ```csharp public class CustomInterceptorAttribute : AbstractInterceptorAttribute { 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"); } } } ``` -------------------------------- ### Benchmark Class for IoC Performance Comparison Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/injector.en.md This C# class uses BenchmarkDotNet to compare the performance of AspectCore's service resolver against Autofac's container. It includes benchmarks for simple object resolution, property injection, and constructor injection. Ensure necessary Autofac and BenchmarkDotNet packages are installed. ```csharp using BenchmarkDotNet.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AspectCore.Extensions.DependencyInjection; using Autofac; namespace AspectCore.Performance { public interface ILogger { } public class Logger : ILogger { } public interface ITaskService { } public class TaskService : ITaskService { } public interface ISampleRepository { } public class SampleRepository : ISampleRepository { public ILogger Logger { get; set; } } public interface ISampleService { } public class SampleService2 : ISampleService { public SampleService2(ILogger logger) { } } [AllStatisticsColumn] [MemoryDiagnoser] public class Benckmarks { private readonly IServiceResolver serviceResolver; private readonly IContainer container; public Benckmarks() { var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType().As().InstancePerDependency(); containerBuilder.RegisterType().As().InstancePerDependency(); containerBuilder.RegisterType().As().InstancePerDependency().PropertiesAutowired(); containerBuilder.RegisterType().As().InstancePerDependency(); container = containerBuilder.Build(); var serviceContext = new ServiceContext(); serviceContext.AddType(Lifetime.Transient); serviceContext.AddType(Lifetime.Transient); serviceContext.AddType(Lifetime.Transient); serviceContext.AddType(Lifetime.Transient); serviceResolver = serviceContext.Build(); } [Benchmark] public object Autofac_Sample_Resolve() { return container.Resolve(); } [Benchmark] public object AspectCore_Sample_Resolve() { return serviceResolver.Resolve(); } [Benchmark] public object Autofac_PropertyInjection() { return container.Resolve(); } [Benchmark] public object AspectCore_PropertyInjection() { return serviceResolver.Resolve(); } [Benchmark] public object Autofac_ConstructorInjection() { return container.Resolve(); } [Benchmark] public object AspectCore_ConstructorInjection() { return serviceResolver.Resolve(); } } } ``` -------------------------------- ### Property Invocation Reflection Extension Usage Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/reflection-extensions.md Use PropertyReflector for property reflection, similar to System.Reflection.PropertyInfo. Get and set property values using the reflector. ```csharp var property = typeof(PropertyFakes).GetTypeInfo().GetProperty("Property"); var reflector = property.GetReflector(); var value = reflector.GetValue(instance); ``` -------------------------------- ### Registering Services in AspectCore Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/injector.en.md Demonstrates registering services using type, instance, and delegate factory methods with IServiceContext. ```csharp IServiceContext services = new ServiceContext(); // Register service using type services.AddType(); // Register service using instance, service lifecycle is limited to singleton services.AddInstance(new TaskService()); // Register service using delegate factory services.AddDelegate(resolver => new TaskService()); ``` -------------------------------- ### Registering Services in AspectCore.Injector Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md Demonstrates how to register services using type, instance, and delegate factory methods with IServiceContext. ```csharp IServiceContext services = new ServiceContext(); // Register service using type services.AddType(); // Register service using instance, service lifecycle limited to singleton services.AddInstance(new TaskService()); // Register service using delegate factory services.AddDelegate(resolver => new TaskService()); ``` -------------------------------- ### Benchmark Results Summary Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md This table presents the performance results from running the benchmark class in Release mode, showing metrics like Mean execution time, Min/Max times, Operations per second, and memory allocation. ```text BenchmarkDotNet=v0.10.8, OS=Windows 10 Threshold 2 (10.0.10586) Processor=Intel Core i5-4590 CPU 3.30GHz (Haswell), ProcessorCount=4 Frequency=3215206 Hz, Resolution=311.0221 ns, Timer=TSC dotnet cli version=2.0.0 [Host] : .NET Core 4.6.00001.0, 64bit RyuJIT DefaultJob : .NET Core 4.6.00001.0, 64bit RyuJIT | Method | Mean | Min | Max | Op/s | Gen 0 | Allocated | |-------------------------------- |------------:|------------:|------------:|-------------:|-------:|----------:| | Autofac_Sample_Resolve | 494.83 ns | 482.52 ns | 506.58 ns | 2,020,908.9 | 0.2384 | 752 B | | AspectCore_Sample_Resolve | 88.52 ns | 87.92 ns | 89.31 ns | 11,296,837.3 | 0.0279 | 88 B | | Autofac_PropertyInjection | 2,014.46 ns | 2,004.18 ns | 2,028.83 ns | 496,411.0 | 0.5875 | 1856 B | | AspectCore_PropertyInjection | 307.55 ns | 303.61 ns | 310.74 ns | 3,251,544.6 | 0.1063 | 336 B | | Autofac_ConstructorInjection | 1,465.71 ns | 1,454.43 ns | 1,480.38 ns | 682,263.5 | 0.6084 | 1920 B | | AspectCore_ConstructorInjection | 284.94 ns | 283.55 ns | 286.05 ns | 3,509,500.8 | 0.0987 | 312 B | ``` -------------------------------- ### Constructor and Property Injection in AspectCore Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md Illustrates constructor injection and property injection using the [FromServiceContext] attribute for dependency injection in AspectCore. ```csharp public interface ISampleService { } public class SampleService : ISampleService { private readonly ISampleRepository _sampleRepository; private readonly ILogger _logger; // Constructor injection public SampleService(ISampleRepository sampleRepository, ILogger logger) { _sampleRepository = sampleRepository; _logger = logger; } } public interface ISampleRepository { } public class SampleRepository : ISampleRepository { // Property injection. Property injection condition: mark with FromServiceContext attribute and allow set. Properties meeting these conditions are automatically injected [FromServiceContext] public ILogger Logger { get; set; } } ``` -------------------------------- ### Configure Services for AspectCore Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Register the custom service and configure the service collection to create proxy types using AspectCore. Add MVC services and enable dynamic proxy configuration. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddMvc(); services.ConfigureDynamicProxy(); } ``` -------------------------------- ### Integrating AspectCore.Injector in ASP.NET Core Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/injector.en.md Demonstrates replacing the default ASP.NET Core DependencyInjection with AspectCore.Injector using two lines of code. ```csharp public IServiceProvider ConfigureServices(IServiceCollection services) { // Add your services... // Add services from IServiceCollection to ServiceContainer var container = services.ToServiceContainer(); return container.Build(); } ``` -------------------------------- ### Resolving Services with AspectCore Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/injector.en.md Shows how to resolve single services, required services, and collections of services using IServiceResolver. ```csharp // Create service resolver IServiceResolver serviceResolver = services.Build(); // Resolve single service ISampleService sampleService = serviceResolver.Resolve(); // Resolve single service and verify if it's null, throw exception if null ISampleService sampleServiceRequired = serviceResolver.ResolveRequired(); // Resolve service collection, returns empty collection if not registered IEnumerable sampleServices = serviceResolver.ResolveMany(); ``` -------------------------------- ### Set Service Provider Factory in Program.cs Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Configure the host builder to use the DynamicProxyServiceProviderFactory. This is essential for AspectCore's proxy generation to work correctly. ```csharp public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }) // ... .UseServiceProviderFactory(new DynamicProxyServiceProviderFactory()); ``` -------------------------------- ### Resolving Services with AspectCore.DependencyInjection Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md Shows how to resolve services using IServiceResolver, including resolving single instances, required instances, and collections. ```csharp // Create service resolver IServiceResolver serviceResolver = services.Build(); // Resolve single service ISampleService sampleService = serviceResolver.Resolve(); // Resolve single service and verify if null, throw exception if null ISampleService sampleServiceRequired = serviceResolver.ResolveRequired(); // Resolve service collection, returns empty collection if not registered IEnumerable sampleServices = serviceResolver.ResolveMany(); ``` -------------------------------- ### Integrating AspectCore.Injector with ASP.NET Core Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md Shows how to replace the default ASP.NET Core Dependency Injection with AspectCore.Injector by converting IServiceCollection to IServiceContainer and building the container. ```csharp public IServiceProvider ConfigureServices(IServiceCollection services) { // Add your services... // Add services from IServiceCollection to ServiceContainer container var container = services.ToServiceContainer(); return container.Build(); } ``` -------------------------------- ### Constructor Injection in Interceptors Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Illustrates constructor injection for interceptors, which requires the interceptor to be registered as a service in the DI container. ```csharp public class CustomInterceptorAttribute : AbstractInterceptorAttribute { private readonly ILogger ctorlogger; ``` -------------------------------- ### Benchmark Class for IoC Performance Comparison Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/injector.md This C# class uses BenchmarkDotNet to compare the performance of AspectCore's service resolver against Autofac's container for different dependency injection scenarios. ```csharp using BenchmarkDotNet.Attributes; using AspectCore.Extensions.DependencyInjection; using Autofac; using System; [AllStatisticsColumn] [MemoryDiagnoser] public class Benckmarks { private readonly IServiceResolver serviceResolver; private readonly IContainer container; public Benckmarks() { var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType().As().InstancePerDependency(); containerBuilder.RegisterType().As().InstancePerDependency(); containerBuilder.RegisterType().As().InstancePerDependency().PropertiesAutowired(); containerBuilder.RegisterType().As().InstancePerDependency(); container = containerBuilder.Build(); var serviceContext = new ServiceContext(); serviceContext.AddType(Lifetime.Transient); serviceContext.AddType(Lifetime.Transient); serviceContext.AddType(Lifetime.Transient); serviceContext.AddType(Lifetime.Transient); serviceResolver = serviceContext.Build(); } [Benchmark] public object Autofac_Sample_Resolve() { return container.Resolve(); } [Benchmark] public object AspectCore_Sample_Resolve() { return serviceResolver.Resolve(); } [Benchmark] public object Autofac_PropertyInjection() { return container.Resolve(); } [Benchmark] public object AspectCore_PropertyInjection() { return serviceResolver.Resolve(); } [Benchmark] public object Autofac_ConstructorInjection() { return container.Resolve(); } [Benchmark] public object AspectCore_ConstructorInjection() { return serviceResolver.Resolve(); } } ``` -------------------------------- ### Service Interface and Implementation with Interceptor Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Define an interface and its implementation for a service. Apply the custom interceptor attribute to the method that should be intercepted. ```csharp public interface ICustomService { [CustomInterceptor] void Call(); } public class CustomService : ICustomService { public void Call() { Console.WriteLine("service calling..."); } } ``` -------------------------------- ### Custom Interceptor with Service Locator Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Demonstrates using the service locator pattern within an interceptor to retrieve services like `ILogger` from the `AspectContext`'s `ServiceProvider`. ```csharp public class CustomInterceptorAttribute : AbstractInterceptorAttribute { public override Task Invoke(AspectContext context, AspectDelegate next) { var logger = context.ServiceProvider.GetService>(); logger.LogInformation("call interceptor"); return next(context); } } ``` -------------------------------- ### Apply Global Interceptor to Services Using Wildcards Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Register a global interceptor that applies to services matching a wildcard pattern, such as those ending with 'Service'. ```csharp services.ConfigureDynamicProxy(config => { config.Interceptors.AddTyped(Predicates.ForService("*Service")); }); ``` -------------------------------- ### Configuring AOP Interceptors Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/injector.en.md Shows how to add typed interceptors to the AspectCore service container configuration. ```csharp services.Configure(config => { config.Interceptors.AddTyped(); }); ``` -------------------------------- ### Configure Global Non-Aspect Rules Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Configure global rules to exclude namespaces, services, or methods from interception using predicates. ```csharp services.ConfigureDynamicProxy(config => { // Services under the App1 namespace will not be proxied config.NonAspectPredicates.AddNamespace("App1"); // Services under namespaces ending in App1 will not be proxied config.NonAspectPredicates.AddNamespace("*.App1"); // ICustomService interface will not be proxied config.NonAspectPredicates.AddService("ICustomService"); // Interfaces and classes with Service suffix will not be proxied config.NonAspectPredicates.AddService("*Service"); // Methods named Query will not be proxied config.NonAspectPredicates.AddMethod("Query"); // Methods with Query suffix will not be proxied config.NonAspectPredicates.AddMethod("*Query"); }); ``` -------------------------------- ### Constructor Reflection Extension Usage Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/reflection-extensions.md Use ConstructorReflector for constructor reflection, similar to System.Reflection.ConstructorInfo. Invoke constructors using the reflector. ```csharp var constructorInfo = typeof(ConstructorFakes).GetTypeInfo().GetConstructor(new Type[0]); var reflector = constructorInfo.GetReflector(); var instance = reflector.Invoke(args); ``` -------------------------------- ### Property Injection in Interceptors Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/1.Getting-Started.md Demonstrates property injection in an interceptor using the FromServiceContextAttribute. This requires the interceptor to be registered using AddTyped. ```csharp public class CustomInterceptorAttribute : AbstractInterceptorAttribute { // ps: Property injection only works when using config.Interceptors.AddTyped(); // It does not work with services.AddSingleton(); + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced(); }); [FromServiceContext] public ILogger Logger { get; set; } public override Task Invoke(AspectContext context, AspectDelegate next) { Logger.LogInformation("call interceptor"); return next(context); } } ``` -------------------------------- ### Property Injection in Interceptors Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Demonstrates property injection using [FromServiceContextAttribute] for dependencies like ILogger within an interceptor. Note that this works with AddTyped but not with AddServiced when the interceptor is registered as a singleton. ```csharp public class CustomInterceptorAttribute : AbstractInterceptorAttribute { // Note: Property injection only works when using config.Interceptors.AddTyped(); // It cannot be used with services.AddSingleton() + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced(); }); [FromServiceContext] public ILogger Logger { get; set; } public override Task Invoke(AspectContext context, AspectDelegate next) { Logger.LogInformation("call interceptor"); return next(context); } } ``` -------------------------------- ### Register Interceptor as a Service Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Register an interceptor as a service with dependency injection, allowing it to be resolved by the DI container. ```csharp services.AddTransient(provider => new CustomInterceptorAttribute("custom")); ``` -------------------------------- ### Dependency Injection in Controller Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Inject the custom service interface into a controller's constructor. The service will be resolved by the dependency injection container. ```csharp public class HomeController : Controller { private readonly ICustomService _service; public HomeController(ICustomService service) { _service = service; } public IActionResult Index() { _service.Call(); return View(); } } ``` -------------------------------- ### Register Global Interceptor with Constructor Arguments Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Register a global interceptor with specific constructor arguments using AddTyped. ```csharp services.ConfigureDynamicProxy(config => { config.Interceptors.AddTyped(args: new object[] { "custom" }); }); ``` -------------------------------- ### Configure Global Non-Aspect Rules Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/1.Getting-Started.md Configure global rules to exclude namespaces, services, or methods from AspectCore interception using predicates. ```csharp services.ConfigureDynamicProxy(config => { // Services under App1 namespace won't be proxied config.NonAspectPredicates.AddNamespace("App1"); // Services under namespaces where the last level is App1 won't be proxied config.NonAspectPredicates.AddNamespace("*.App1"); // ICustomService interface won't be proxied config.NonAspectPredicates.AddService("ICustomService"); // Interfaces and classes ending with Service won't be proxied config.NonAspectPredicates.AddService("*Service"); // Methods named Query won't be proxied config.NonAspectPredicates.AddMethod("Query"); // Methods ending with Query won't be proxied config.NonAspectPredicates.AddMethod("*Query"); }); ``` -------------------------------- ### Register Global Interceptor as a Service Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Register a global interceptor that has been configured as a service in the DI container using AddServiced. ```csharp services.ConfigureDynamicProxy(config => { // Add registered service config.Interceptors.AddServiced(); }); ``` -------------------------------- ### Apply Global Interceptor to Specific Methods Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Configure a global interceptor to apply only to methods matching a specific predicate, such as those ending with a certain name. ```csharp services.ConfigureDynamicProxy(config => { config.Interceptors.AddTyped(method => method.Name.EndsWith("MethodName")); }); ``` -------------------------------- ### C# Interceptor Implementation Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/0.AOP-Introduction.en.md This C# code defines an interceptor class that logs messages before and after a method call, and handles exceptions. It implements the IInterceptor interface. ```csharp public class Interceptor : IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine("Before target call"); try { invocation.Proceed(); } catch(Exception) { Console.WriteLine("Target threw an exception!"); throw; } finally { Console.WriteLine("After target call"); } } } ``` -------------------------------- ### Method Invocation Reflection Extension Usage Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/en/reflection-extensions.md Use MethodReflector for method reflection, similar to System.Reflection.MethodInfo. Invoke methods using the reflector. ```csharp var typeInfo = typeof(MethodFakes).GetTypeInfo(); var method = typeInfo.GetMethod("Call"); var refector = method.GetReflector(); refector.Invoke(instance,args); ``` -------------------------------- ### Prevent Interception with NonAspectAttribute Source: https://github.com/dotnetcore/aspectcore-framework/blob/master/docs/1.Getting-Started.en.md Use the NonAspectAttribute on an interface to prevent it and its implementations from being proxied. ```csharp [NonAspect] public interface ICustomService { void Call(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.