在同一类中的另一个方法中调用方法时,拦截器不起作用



我已经使用AutofacDynamicProxy来实现方法的缓存。但inceptor在调用同一类中另一个方法内部的方法时不起作用。例如,我的ClassRepository类为:

public class ClassRepository : IClassRepository
{
[Cache]
public async Task<List<string>> Method1()
{
//Do some thing
}
[Cache]
public async Task<List<string>> Method2()
{
var methodCall = await Method1()
}
}

我的感受器是这样的:

public class CacheInterceptor : IInterceptor
{
private readonly ICache cache;
private static ConcurrentDictionary<string, bool> InProcessKeys = new ConcurrentDictionary<string, bool>();
public CacheInterceptor(ICache cache)
{
this.cache = cache;
}
public void Intercept(IInvocation invocation)
{
ProcessInterceptAsync(invocation).Wait();

}
private async Task ProcessInterceptAsync(IInvocation invocation)
{
var proceed = invocation.CaptureProceedInfo();

var cacheAttribute = invocation.MethodInvocationTarget.GetCustomAttributes<CacheAttribute>(false).FirstOrDefault();
if (cacheAttribute == null)
{
proceed.Invoke();
return;
}
var key = GetCacheKey(invocation);
ExistKeyCheck(key);
var methodReturnType = invocation.Method.ReturnType;
dynamic cacheResult = GetCache(key, cacheAttribute, methodReturnType);
if (cacheResult != null)
{
invocation.ReturnValue = cacheResult;
InProcessKeys.Remove(key, out _);
return;
}
InProcessKeys.TryAdd(key, true);
proceed.Invoke();
var delegateType = GetDelegateType(invocation);
switch (delegateType)
{
case MethodType.Synchronous:
break;
case MethodType.AsyncAction:
await InterceptAsync((Task)invocation.ReturnValue);
break;
case MethodType.AsyncFunction:
var method = invocation.MethodInvocationTarget;
var isAsync = method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null;
if (isAsync && typeof(Task).IsAssignableFrom(method.ReturnType))
{
var methodResult = await InterceptAsync((dynamic)invocation.ReturnValue);
invocation.ReturnValue = methodResult;
}
break;
default:
break;
}
if (invocation.ReturnValue == null)
{
InProcessKeys.Remove(key, out _);
return;
}
await cache.SetAsync(key, invocation.ReturnValue, cacheAttribute.Duration, cacheAttribute.CacheInstance, cacheAttribute.Extend);
InProcessKeys.Remove(key, out _);
}
private dynamic GetCache(string key, CacheAttribute cacheAttribute, Type methodReturnType)
{
var finalType = methodReturnType.GetGenericArguments()[0];
var getMethod = typeof(DistributedCache).GetMethods().Where(x => x.IsGenericMethod && x.Name =="Get").FirstOrDefault().MakeGenericMethod(finalType);
var cacheResult = (dynamic)getMethod.Invoke(cache, new object[] { key, cacheAttribute.CacheInstance });
if (cacheResult is null) return null;
if (methodReturnType.GetGenericTypeDefinition() == typeof(Task<>))
return Task.FromResult(cacheResult);
else
return cacheResult;
}
private static void ExistKeyCheck(string key)
{
if (InProcessKeys.Any(x => x.Key==key))
{
Task.Delay(50).Wait();
var counter = 0;
while (InProcessKeys.Any(x => x.Key==key) && counter < 10)
{
Task.Delay(50).Wait();
counter++;
}
}
}
private static async Task InterceptAsync(Task task) => await task.ConfigureAwait(false);
private static async Task<T> InterceptAsync<T>(Task<T> task) => await task.ConfigureAwait(false);
private MethodType GetDelegateType(IInvocation invocation)
{
var returnType = invocation.Method.ReturnType;
if (returnType == typeof(Task))
return MethodType.AsyncAction;
if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
return MethodType.AsyncFunction;
return MethodType.Synchronous;
}
private enum MethodType
{
Synchronous,
AsyncAction,
AsyncFunction
}
private static string GetCacheKey(IInvocation invocation)
{
var key = invocation.Arguments.Length > 0 ? $"-{invocation.Arguments[0]}" : "";
var cacheKey = $"{invocation.TargetType.FullName.Replace('.', ':')}" +
$".{invocation.Method.Name}" +
$"{key}";
return cacheKey;
}
}

我的autofac模块是这样的:

public class RepositoryModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{

builder.RegisterType<CacheInterceptor>();
var flightDataAccess = Assembly.Load("DataAccess");
builder.RegisterAssemblyTypes(flightDataAccess)
.Where(x => x.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope().InterceptedBy(typeof(CacheInterceptor)).EnableInterfaceInterceptors();

}
}

当我解析IClassRepository并调用Method2时,CacheInterceptor为Method2成功执行。但在Method2内部,我调用了Method1,但它对Method1不起作用。如果有人能帮忙,我将不胜感激。

您的问题在这里:

public async void Intercept(IInvocation invocation)
...
invocation.Proceed();

您需要开发一个没有async void的解决方案。看一下文档,它应该会给你一个很好的起点。您需要设置ReturnValue并调用CaptureProceedInfo

过了一段时间,我就可以解决了。起初,我不得不将我的方法更改为virtual方法。

注意:对于私有方法,我将它们更改为protected virtual

最终更改为:

public class ClassRepository : IClassRepository
{
[Cache]
public virtual async Task<List<string>> Method1()
{
//Do some thing
}
[Cache]
public virtual async Task<List<string>> Method2()
{
var methodCall = await Method1()
}
}

Module类中,我将EnableInterfaceInterceptors更改为EnableClassInterceptors,最终更改为:

public class RepositoryModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{

builder.RegisterType<CacheInterceptor>();
var flightDataAccess = Assembly.Load("DataAccess");
builder.RegisterAssemblyTypes(flightDataAccess)
.Where(x => x.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope().InterceptedBy(typeof(CacheInterceptor)).EnableClassInterceptors();

}
}

最新更新