C# PostSharp缓存方法使用ASP.NET内核内存缓存拦截Spect
如何从任何类(包括PostSharp方面)访问ASP.NET Core中的内存缓存?例如,我需要在C# PostSharp缓存方法使用ASP.NET内核内存缓存拦截Spect,c#,.net-core,postsharp,C#,.net Core,Postsharp,如何从任何类(包括PostSharp方面)访问ASP.NET Core中的内存缓存?例如,我需要在methodinterceptionspect和OnMethodBoundaryAspect中访问,我在这里假设您正在使用内置的ASP.NET核心依赖项注入和IMemoryCache实现。然而,该示例可以容易地适用于其他实现。我将选择解决方面依赖关系的方法。下面是文档页面中的修改示例 public class CacheAttribute : MethodInterceptionAspect {
methodinterceptionspect
和OnMethodBoundaryAspect
中访问,我在这里假设您正在使用内置的ASP.NET核心依赖项注入和IMemoryCache实现。然而,该示例可以容易地适用于其他实现。我将选择解决方面依赖关系的方法。下面是文档页面中的修改示例
public class CacheAttribute : MethodInterceptionAspect
{
public override void OnInvoke(MethodInterceptionArgs methodInterceptionArgs)
{
if ((methodInterceptionArgs.Method.Name == CacheAspectAction.Get.ToString())
//&& (Memory.Cache[cacheKey] != null)
)
{
// methodInterceptionArgs.ReturnValue = HttpRuntime.Cache[cacheKey];
return;
}
object returnVal = methodInterceptionArgs.Invoke(methodInterceptionArgs.Arguments);
ClanCache(cacheKeyBase, cacheKey);
if (returnVal != null)
//Memory.Cache.Insert(cacheKey, returnVal, null, expirationInformation.AbsoluteExpiration, expirationInformation.SlidingExpiration);
methodInterceptionArgs.ReturnValue = returnVal;
}
}
//使用内置ASP.NET核心服务提供程序解析服务的帮助器类。
公共静态类AspectServiceLocator
{
专用静态IServiceProvider服务提供商;
公共静态无效初始化(IWebHost主机)
{
serviceProvider=host.Services;
}
公共静态惰性GetService(),其中T:class
{
返回新的Lazy(GetServiceImpl);
}
私有静态T GetServiceImpl()
{
if(serviceProvider==null)
抛出新的InvalidOperationException();
return(T)serviceProvider.GetService(typeof(T));
}
}
公共课程
{
公共静态void Main(字符串[]args)
{
IWebHost主机=CreateWebHostBuilder(args).Build();
//在ASP.NET核心程序启动期间初始化AspectServiceLocator
AspectServiceLocator.Initialize(主机);
host.Run();
}
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup();
}
[可序列化]
公共类CacheAttribute:MethodInterceptionSpect
{
私有静态惰性缓存;
静态缓存属性()
{
//使用AspectServiceLocator在应用程序运行时初始化缓存服务字段。
如果(!postGripperEnvironment.IsPostSharpRunning)
{
cache=AspectServiceLocator.GetService();
}
}
公共覆盖无效OnInvoke(MethodInterceptionArgs args)
{
对象cacheKey=args.Method.Name;
对象缓存结果;
if(cache.Value.TryGetValue(cacheKey,out cachedResult))
{
args.ReturnValue=cachedResult;
返回;
}
args.procedure();
cache.Value.Set(cacheKey,args.ReturnValue);
}
}
您能否提供更多关于您面临的问题的信息,以及到目前为止您尝试了哪些解决方案?这是关于访问通过依赖项注入提供的ASP.NET核心实例吗?在本例中,我可以向您介绍有关使用方面依赖项的各种技术的文档:是的,它是关于通过依赖项注入提供的ASP.NET Core IMemoryCache实例的,我确实阅读了您发送给我的链接,并搜索了一个更详细的信息,但仍然不确定如何执行
// A helper class that resolves services using built-in ASP.NET Core service provider.
public static class AspectServiceLocator
{
private static IServiceProvider serviceProvider;
public static void Initialize(IWebHost host)
{
serviceProvider = host.Services;
}
public static Lazy<T> GetService<T>() where T : class
{
return new Lazy<T>(GetServiceImpl<T>);
}
private static T GetServiceImpl<T>()
{
if (serviceProvider == null)
throw new InvalidOperationException();
return (T) serviceProvider.GetService(typeof(T));
}
}
public class Program
{
public static void Main(string[] args)
{
IWebHost host = CreateWebHostBuilder(args).Build();
// Initialize the AspectServiceLocator during ASP.NET Core program start-up
AspectServiceLocator.Initialize(host);
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
[PSerializable]
public class CacheAttribute : MethodInterceptionAspect
{
private static Lazy<IMemoryCache> cache;
static CacheAttribute()
{
// Use AspectServiceLocator to initialize the cache service field at application run-time.
if (!PostSharpEnvironment.IsPostSharpRunning)
{
cache = AspectServiceLocator.GetService<IMemoryCache>();
}
}
public override void OnInvoke(MethodInterceptionArgs args)
{
object cacheKey = args.Method.Name;
object cachedResult;
if (cache.Value.TryGetValue(cacheKey, out cachedResult))
{
args.ReturnValue = cachedResult;
return;
}
args.Proceed();
cache.Value.Set(cacheKey, args.ReturnValue);
}
}