diff --git a/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs b/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs index e3143d027c7dc152cff9206ba02ab98b3e6adf52..bc1585b3ec969ba99539c79e1ed10a8a5cf3f45a 100644 --- a/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs +++ b/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs @@ -26,18 +26,19 @@ namespace Microsoft.Extensions.DependencyInjection options.ImageOption.FontFamily = DefaultFontFamilys.Instance.GetFontFamily(fontFamily); } }); - if (optionsAction != null) services.PostConfigure(optionsAction); + if (optionsAction != null) services.PostConfigure(optionsAction); + + services.TryAdd(ServiceDescriptor.Scoped()); + services.AddScoped(); + services.AddDistributedMemoryCache(); - services.TryAdd(ServiceDescriptor.Scoped()); return services; } + [Obsolete("请直接使用 AddCaptcha 方法,已更改为默认注册内存缓存", false)] public static IServiceCollection AddMemoryCacheCaptcha(this IServiceCollection services, IConfiguration configuration, Action optionsAction = default!) { - return services - .AddDistributedMemoryCache() - .AddScoped() - .AddCaptcha(configuration, optionsAction); + return services.AddCaptcha(configuration, optionsAction); } } } \ No newline at end of file diff --git a/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs b/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs index 4209a08d4c45ea2399eb7b0a6cfc8500935d8642..5a5b48e27bec81cafe378d2274d359e87a17803f 100644 --- a/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs +++ b/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs @@ -10,21 +10,23 @@ namespace Microsoft.Extensions.DependencyInjection { public static class RedisCacheCaptchaExtensions { + /// + /// 即将弃用,调整为直接使用 AddCache(),并且配置 AddStackExchangeRedisCache,参考 https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed?view=aspnetcore-6.0#distributed-redis-cache + /// + [Obsolete("调整为直接使用 AddCache(),使用 Redis 缓存时配置 AddStackExchangeRedisCache,参考 https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed?view=aspnetcore-6.0#distributed-redis-cache",false)] public static IServiceCollection AddRedisCacheCaptcha(this IServiceCollection services, IConfiguration configuration, Action optionsAction = default!, string connName = null!) { - var connStr = configuration.GetConnectionString(connName??"RedisCache"); + var connStr = configuration.GetConnectionString(connName ?? "RedisCache"); if (string.IsNullOrEmpty(connStr)) { throw new Exception("Please set connection string for key `RedisCache` in setting json file"); } - return services - .AddStackExchangeRedisCache(options => - { - options.Configuration = connStr; - options.InstanceName = "captcha:"; - }) - .AddScoped() - .AddCaptcha(configuration, optionsAction); + services.AddCaptcha(configuration, optionsAction); + return services.AddStackExchangeRedisCache(options => + { + options.Configuration = connStr; + options.InstanceName = "captcha:"; + }); } } } \ No newline at end of file diff --git a/Lazy.Captcha.Web/Program.cs b/Lazy.Captcha.Web/Program.cs index 42f573bdadca72fe33bd47466f0fa7b321f8c733..0908969441ea540cf4529f91823a5c18ea05b655 100644 --- a/Lazy.Captcha.Web/Program.cs +++ b/Lazy.Captcha.Web/Program.cs @@ -3,20 +3,23 @@ using Lazy.Captcha.Core.Generator; var builder = WebApplication.CreateBuilder(args); -//// redis洢appsettings.json + //builder.Services.AddRedisCacheCaptcha(builder.Configuration); + // ڴ洢 appsettings.json -builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option => -{ - // ÿԸappsettings.json +builder.Services.AddCaptcha(builder.Configuration); + + +// ʹredis棬 +//builder.Services.AddStackExchangeRedisCache(options => +//{ +// options.Configuration = builder.Configuration.GetConnectionString("RedisCache"); +// options.InstanceName = "captcha:"; +//}); - //option.ImageOption.Animation = false; - //option.CaptchaType = CaptchaType.WORD_UPPER; - //option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Robot; - //option.ImageOption.BubbleCount = 0; -}); +// ----------------------------------------------------------------------------- // ȫòڴ //builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option => //{