public class MyInterceptor : IAbpInterceptor, ITransientDependency
{
public async Task InterceptAsync(IAbpMethodInvocation invocation)
{
var beforeMethodName = (AopMethodAttribute) invocation.Method.GetCustomAttributes(typeof(AopBeforeMethodAttribute), false).FirstOrDefault();
var beforeMethod = GetType().GetDeclaredMethods().FirstOrDefault(_ => _.IsPrivate && _.Name == beforeMethodName?.Name);
beforeMethod?.Invoke(this, new object[] { invocation.Arguments });
await invocation.ProceedAsync();
var afterMethodName = (AopMethodAttribute) invocation.Method.GetCustomAttributes(typeof(AopAfterMethodAttribute), false).FirstOrDefault();
var afterMethod = GetType().GetDeclaredMethods().FirstOrDefault(_ => _.IsPrivate && _.Name == afterMethodName?.Name);
afterMethod?.Invoke(this, new object[] { invocation.Arguments });
}
private async Task BeforeAsync(object[] args)
{
await Task.CompletedTask;
}
private async Task AfterAsync(object[] args)
{
await Task.CompletedTask;
}
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.OnRegistred(registration =>
{
if (registration.ImplementationType.IsSubclassOf(typeof(BookAppService)))
{
registration.Interceptors.Add<MyInterceptor>();
}
});
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public abstract class AopMethodAttribute : Attribute
{
public string Name
{
get; set;
}
}
public sealed class AopBeforeMethodAttribute : AopMethodAttribute
{
}
public sealed class AopAfterMethodAttribute : AopMethodAttribute
{
}
[AopBeforeMethod(Name = "BeforeAsync")]
[AopAfterMethod(Name = "AfterAsync")]
public virtual async Task<bool> NextPatientAsync(string roomName, string patientGuid)
{
if (string.IsNullOrEmpty(patientGuid))
{
return await _patientManager.MovePatientToCallingAsync(this.CurrentUser.Name, roomName);
}
var patient = (await _patientManager.GetQueryableAsync()).FirstOrDefault(_ => _.PatientGuid == patientGuid);
if (patient == null)
{
Log.Error($"Invalid PatientGuid {patientGuid}");
return false;
}
return await _patientManager.NextAsync(this.CurrentUser.Name, patient.Id);
}
IAbpInterceptor应用文档
最新推荐文章于 2024-11-09 16:00:26 发布