using AspNetCoreRateLimit; namespace Extensions { public static class IPRateExtension { public static void AddIPRate(this IServiceCollection services, IConfiguration configuration) { if (services == null) throw new ArgumentNullException(nameof(services)); //从appsettings.json中加载常规配置,IpRateLimiting与配置文件中节点对应 services.Configure(configuration.GetSection("IpRateLimiting")); //从appsettings.json中加载Ip规则 services.Configure(configuration.GetSection("IpRateLimitPolicies")); //注入计数器和规则存储 services.AddSingleton(); services.AddSingleton(); //配置(解析器、计数器密钥生成器) services.AddSingleton(); services.AddSingleton(); } } }