Load rules/client policies from SQL server database instead of appsettings on startup
digglekarl opened this issue ยท 7 comments
Hello,
I am trying to load the rules and client policies from a SQL server database at startup instead of appsettings I found the below link
Could you provide a bit more information on how to achieve this as I have created a class that inherits from MemoryCahceClientPolicy
`public class DatabaseCacheClientPolicyStore : MemoryCacheClientPolicyStore
{
private readonly IRateLimitingService rateLimitingService;
public DatabaseCacheClientPolicyStore(
IMemoryCache cache,
IOptions<ClientRateLimitOptions> options,
IOptions<ClientRateLimitPolicies> policies,
IRateLimitingService rateLimitingService)
: base(cache, options, policies)
{
this.rateLimitingService = rateLimitingService;
}
public new async Task SeedAsync()
{
var policies = await rateLimitingService.GetClientPolicies();
foreach (var rule in policies)
{
await SetAsync($"{rule.ClientId}", new ClientRateLimitPolicy { ClientId = rule.ClientId, Rules = rule.Rules });
}
}
}`
I have then registered this in program.cs
builder.Services.AddSingleton<IClientPolicyStore, DatabaseCacheClientPolicyStore>();
But this SeedAsync is not being overridden
Any help is much appreciated.
Thanks
Karl
How did you configure the rate limiter?
If you're using one of these extensions https://github.com/stefanprodan/AspNetCoreRateLimit/blob/master/src/AspNetCoreRateLimit/StartupExtensions.cs, you need to remove it and manually add the services
How did you configure the rate limiter? If you're using one of these extensions https://github.com/stefanprodan/AspNetCoreRateLimit/blob/master/src/AspNetCoreRateLimit/StartupExtensions.cs, you need to remove it and manually add the services
I have configured as this
`
#region RateLimitingMiddleware
builder.Services.AddMemoryCache();
builder.Services.Configure(builder.Configuration.GetSection("ClientRateLimiting"));
builder.Services.Configure(builder.Configuration.GetSection("ClientRateLimitPolicies"));
builder.Services.AddSingleton<IClientPolicyStore, DatabaseCacheClientPolicyStore>();
builder.Services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
builder.Services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
builder.Services.AddSingleton<IRateLimitConfiguration, CustomRateLimitConfiguration>();
builder.Services.AddInMemoryRateLimiting();
#endregion`
but I commented out the 2 lines
`builder.Services.Configure(builder.Configuration.GetSection("ClientRateLimiting"));
builder.Services.Configure(builder.Configuration.GetSection("ClientRateLimitPolicies"));`
THanks
You need to remove this: builder.Services.AddInMemoryRateLimiting();
You need to remove this: builder.Services.AddInMemoryRateLimiting();
Thank you so much.
Its now calling my class but not the seedasync do I need to do something like this?
`
var clientPolicyStore = app.Services.GetService();
// seed client data
await clientPolicyStore?.SeedAsync()!;
`
Yes, you need to call it yourself when the application starts
Hello,
So I have managed to get this sort of working but the client policies are not taking effect when I use the extensions and pull the policies from appsettings it works fine but when I use the above I can see the policies are in the cache but they're not being applied.
Thanks
I have fixed this I was missing the ClientPolicyPrefix
Thanks again for all your help
Much appreciated