gpt4 book ai didi

.net-core - .NET Core 默认依赖注入(inject)与 CaSTLe DynamicProxy

转载 作者:行者123 更新时间:2023-12-05 08:18:35 28 4
gpt4 key购买 nike

我有许多 AOP 库使用 CaSTLe DynamicProxy 和 Autofac DI 容器来进行日志记录、审计、事务控制等。

我想知道是否有一种方法可以使用默认的 .NET Core DI 容器来声明拦截器。拥有这种灵 active 会很好,因为许多 .NET Core 项目不使用 Autofac。

最佳答案

是的,您可以通过核心 DI 使用 DynamicProxy。我在 http://codethug.com/2021/03/17/Caching-with-Attributes-in-DotNet-Core5/ 上写了一篇解释它的博文。 ,但这是它的代码:

创建属性

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CacheAttribute : Attribute
{
public int Seconds { get; set; } = 30;
}

创建拦截器(需要CaSTLe.Core nuget包)

public class CacheInterceptor : IInterceptor
{
private IMemoryCache _memoryCache;
public CacheInterceptor(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}

// Create a cache key using the name of the method and the values
// of its arguments so that if the same method is called with the
// same arguments in the future, we can find out if the results
// are cached or not
private static string GenerateCacheKey(string name,
object[] arguments)
{
if (arguments == null || arguments.Length == 0)
return name;
return name + "--" +
string.Join("--", arguments.Select(a =>
a == null ? "**NULL**" : a.ToString()).ToArray());
}

public void Intercept(IInvocation invocation)
{
var cacheAttribute = invocation.MethodInvocationTarget
.GetCustomAttributes(typeof(CacheAttribute), false)
.FirstOrDefault() as CacheAttribute;

// If the cache attribute is added ot this method, we
// need to intercept this call
if (cacheAttribute != null)
{
var cacheKey = GenerateCacheKey(invocation.Method.Name,
invocation.Arguments);
if (_memoryCache.TryGetValue(cacheKey, out object value))
{
// The results were already in the cache so return
// them from the cache instead of calling the
// underlying method
invocation.ReturnValue = value;
}
else
{
// Get the result the hard way by calling
// the underlying method
invocation.Proceed();
// Save the result in the cache
var options = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow =
new System.TimeSpan(hours: 0, minutes: 0,
seconds: cacheAttribute.Seconds)
};
_memoryCache.Set(cacheKey, invocation.ReturnValue,
options);
}
}
else
{
// We don't need to cache the results,
// nothing to see here
invocation.Proceed();
}
}
}

添加一个扩展方法来帮助在 DI 中注册类:

public static void AddProxiedScoped<TInterface, TImplementation>
(this IServiceCollection services)
where TInterface : class
where TImplementation : class, TInterface
{
// This registers the underlying class
services.AddScoped<TImplementation>();
services.AddScoped(typeof(TInterface), serviceProvider =>
{
// Get an instance of the Castle Proxy Generator
var proxyGenerator = serviceProvider
.GetRequiredService<ProxyGenerator>();
// Have DI build out an instance of the class that has methods
// you want to cache (this is a normal instance of that class
// without caching added)
var actual = serviceProvider
.GetRequiredService<TImplementation>();
// Find all of the interceptors that have been registered,
// including our caching interceptor. (you might later add a
// logging interceptor, etc.)
var interceptors = serviceProvider
.GetServices<IInterceptor>().ToArray();
// Have Castle Proxy build out a proxy object that implements
// your interface, but adds a caching layer on top of the
// actual implementation of the class. This proxy object is
// what will then get injected into the class that has a
// dependency on TInterface
return proxyGenerator.CreateInterfaceProxyWithTarget(
typeof(TInterface), actual, interceptors);
});
}

将这些行添加到 Startup.cs 中的 ConfigureServices

// Setup Interception
services.AddSingleton(new ProxyGenerator());
services.AddScoped<IInterceptor, CacheInterceptor>(

之后,如果要使用缓存拦截器,需要做两件事:

首先,将属性添加到您的方法

[Cache(Seconds = 30)]
public async Task<IEnumerable<Person>> GetPeopleByLastName(string lastName)
{
return SomeLongRunningProcess(lastName);
}

其次,使用代理/拦截在 DI 中注册类:

services.AddProxiedScoped<IPersonRepository, PersonRepository>();

而不是没有代理/拦截的正常方式:

services.AddScoped<IPersonRepository, PersonRepository>();

关于.net-core - .NET Core 默认依赖注入(inject)与 CaSTLe DynamicProxy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60610580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com