正確使用 HttpClient 使用 HttpClient 註意事項 HttpClient預設最大併發連接數是2 本機測試(被請求的WebApi部署在本機)HttpClient不會被限制最大併發連接數 使用HttpClient要寫個工廠類,因為HttpClient不能頻繁創建 HttpClient類 ...
為了能夠通過配置文件(appsettings.json)或通過代碼進行背景圖片與模板進行配置、可自定義資源類型、自定義驗證規則,本節創建一些擴展類,用來實現這些功能。
上一節內容:NET 6 實現滑動驗證碼(三)、介面
擴展類都放在了Extensions文件夾下
目錄- CaptchaBuilderExtensions.cs
- CaptchaServiceCollectionExtensions.cs
- ServiceCollectionExtensions.cs
- SlideCaptchaException.cs
CaptchaBuilderExtensions.cs
CaptchaBuilderExtensions.cs實現了自定義提供驗證碼背景圖片資源或模板資源、自定義實現資源類型、替換預設驗證規則、禁用預設凹槽模板
using SlideCaptcha;
using SlideCaptcha.Interface;
using SlideCaptcha.Resources.Provider;
using System.Linq;
namespace Microsoft.Extensions.DependencyInjection
{
public static class CaptchaBuilderExtensions
{
/// <summary>
/// 自定義提供驗證碼背景圖片資源或模板資源
/// </summary>
/// <typeparam name="TProvider">自定義實現</typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static CaptchaBuilder AddResourceProvider<TProvider>(this CaptchaBuilder builder) where TProvider : class, IResourceProvider
{
builder.Services.AddSingleton<IResourceProvider, TProvider>();
return builder;
}
/// <summary>
/// 自定義實現資源類型
/// </summary>
/// <typeparam name="THandler">自定義實現</typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static CaptchaBuilder AddResourceHandler<THandler>(this CaptchaBuilder builder) where THandler : class, IResourceHandler
{
builder.Services.AddSingleton<IResourceHandler, THandler>();
return builder;
}
/// <summary>
/// 替換預設的驗證規則
/// </summary>
/// <typeparam name="TValidator">自定義驗證規則</typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static CaptchaBuilder ReplaceValidator<TValidator>(this CaptchaBuilder builder) where TValidator : class, IValidator
{
builder.Services.Replace<IValidator, TValidator>();
return builder;
}
/// <summary>
/// 禁用預設凹槽模板
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static CaptchaBuilder DisableDefaultTemplates(this CaptchaBuilder builder)
{
var serviceDescriptor = builder.Services.FirstOrDefault(e => e.ImplementationType == typeof(EmbeddedResourceProvider));
if (serviceDescriptor != null)
{
builder.Services.Remove(serviceDescriptor);
}
return builder;
}
}
}
CaptchaServiceCollectionExtensions.cs
CaptchaServiceCollectionExtensions.cs為自動註入必要的介面
using Microsoft.Extensions.Configuration;
using SlideCaptcha;
using SlideCaptcha.Constant;
using SlideCaptcha.Generator;
using SlideCaptcha.Interface;
using SlideCaptcha.Resources;
using SlideCaptcha.Resources.Handler;
using SlideCaptcha.Resources.Provider;
using SlideCaptcha.Storage;
using SlideCaptcha.Validator;
using System;
namespace Microsoft.Extensions.DependencyInjection
{
public static class CaptchaServiceCollectionExtensions
{
/// <summary>
/// 依賴註入必要的介面。
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <param name="optionsAction"></param>
/// <returns></returns>
public static CaptchaBuilder AddSlideCaptcha(this IServiceCollection services, IConfiguration configuration, Action<CaptchaOptions> optionsAction = default)
{
services.Configure<CaptchaOptions>(configuration?.GetSection("SlideCaptcha"));
//對所需驗證的參數進行檢查,如果參數不合法就拋一個異常出來
if (optionsAction != null) services.PostConfigure(optionsAction);
var builder = new CaptchaBuilder(services);
services.AddSingleton<IResourceProvider, OptionsResourceProvider>();
services.AddSingleton<IResourceProvider, EmbeddedResourceProvider>();
services.AddSingleton<IResourceHandlerManager, CachedResourceHandlerManager>();
services.AddSingleton<IResourceManager, DefaultResourceManager>();
services.AddSingleton<ISliderCaptchaImageGenerator, SliderCaptchaImageGenerator>();
services.AddSingleton<IResourceHandler, FileResourceHandler>();
services.AddSingleton<IResourceHandler, EmbeddedResourceHandler>();
services.AddScoped<ICaptcha, ImageCaptcha>();
services.AddScoped<IStorage, DefaultStorage>();
services.AddScoped<IValidator, SimpleValidator>();
return builder;
}
}
}
ServiceCollectionExtensions.cs
ServiceCollectionExtensions.cs用於實現IServiceCollection的Replace方法
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection Replace<TService, TImplementation>(this IServiceCollection services)
where TImplementation : TService
{
return services.Replace<TService>(typeof(TImplementation));
}
public static IServiceCollection Replace<TService>(this IServiceCollection services, Type implementationType)
{
return services.Replace(typeof(TService), implementationType);
}
public static IServiceCollection Replace(this IServiceCollection services, Type serviceType, Type implementationType)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (serviceType == null)
{
throw new ArgumentNullException(nameof(serviceType));
}
if (implementationType == null)
{
throw new ArgumentNullException(nameof(implementationType));
}
if (!services.TryGetDescriptors(serviceType, out var descriptors))
{
throw new ArgumentException($"No services found for {serviceType.FullName}.", nameof(serviceType));
}
foreach (var descriptor in descriptors)
{
var index = services.IndexOf(descriptor);
services.Insert(index, descriptor.WithImplementationType(implementationType));
services.Remove(descriptor);
}
return services;
}
private static bool TryGetDescriptors(this IServiceCollection services, Type serviceType, out ICollection<ServiceDescriptor> descriptors)
{
return (descriptors = services.Where(service => service.ServiceType == serviceType).ToArray()).Any();
}
private static ServiceDescriptor WithImplementationType(this ServiceDescriptor descriptor, Type implementationType)
{
return new ServiceDescriptor(descriptor.ServiceType, implementationType, descriptor.Lifetime);
}
}
}
SlideCaptchaException.cs
SlideCaptchaException.cs 實現自定義異常信息。
using System;
namespace SlideCaptcha.Exceptions
{
public class SlideCaptchaException : Exception
{
public SlideCaptchaException() : base()
{
}
public SlideCaptchaException(string message) : base(message)
{
}
public SlideCaptchaException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
歡迎大家關註我的微信公眾號,一起進步,一起成長下載方式:
掃描公眾號二維碼,或搜索公眾號fenxiang3389關註我,回覆captcha
下載,壓縮包包含了驗證碼類庫、服務端API、HTML+JQuery完整代碼、vue3組件代碼及演示代碼!
![](https://img2023.cnblogs.com/blog/93324/202212/93324-20221205182127693-712634750.png)