English | Español
This guide will walk you through the process of setting up and using BeyondNet.Aop to intercept method calls in your application.
Install the core packages via the NuGet Package Manager or .NET CLI:
dotnet add package BeyondNet.Aop
dotnet add package BeyondNet.Aop.DispatchProxy
dotnet add package BeyondNet.Aop.Microsoft.Extensions.DependencyInjection.Aspects.InstallerFirst, let's create a standard business logic service and its interface. Note that DispatchProxy requires the target to be an interface.
public interface ICalculatorService
{
int Add(int a, int b);
}
public class CalculatorService : ICalculatorService
{
public int Add(int a, int b)
{
Console.WriteLine("Executing actual business logic...");
return a + b;
}
}To create an aspect, you need two things: an Attribute to mark the methods, and an Aspect implementation containing the logic.
using BeyondNet.Aop;
public class AuditAttribute : AbstractAspectAttribute
{
public string OperationName { get; set; }
public AuditAttribute(string operationName)
{
OperationName = operationName;
}
}Inherit from AbstractAspect<TAttribute>. You can override methods like OnEntry, OnSuccess, OnException, or OnExit.
using BeyondNet.Aop;
using System;
public class AuditAspect : AbstractAspect<AuditAttribute>
{
protected override void OnEntry(IJoinPoint joinPoint)
{
var attribute = GetAttribute(joinPoint);
Console.WriteLine($"[Audit] Starting operation: {attribute.OperationName}");
Console.WriteLine($"[Audit] Target Method: {joinPoint.MethodInfo.Name}");
}
protected override void OnSuccess(IJoinPoint joinPoint)
{
Console.WriteLine($"[Audit] Operation completed successfully! Result: {joinPoint.Return}");
}
}Apply the [Audit] attribute to the interface method you want to intercept:
public interface ICalculatorService
{
[Audit("Addition Operation")]
int Add(int a, int b);
}Use the AddProxy extension method to register your service into the IServiceCollection. This will automatically wrap the service in the AopProxy and link the aspects.
using Microsoft.Extensions.DependencyInjection;
using BeyondNet.Aop.Microsoft.Extensions.DependencyInjection.Aspects.Installer;
var services = new ServiceCollection();
// Register the Aspect
services.AddTransient<AuditAspect>();
// Register the Service Wrapped in the Proxy
services.AddProxy<ICalculatorService, CalculatorService>();
var provider = services.BuildServiceProvider();When you resolve the service and call the method, you will see the interception in action:
var calculator = provider.GetRequiredService<ICalculatorService>();
// This call will be intercepted!
var result = calculator.Add(5, 10);Output:
[Audit] Starting operation: Addition Operation
[Audit] Target Method: Add
Executing actual business logic...
[Audit] Operation completed successfully! Result: 15