You've already forked Extensions.Configuration.EntityFrameworkCore
3.4 KiB
3.4 KiB
RAIC.Extensions.Configuration.EntityFrameworkCore.SqlServer
This library enhances RAIC.Extensions.Configuration.EntityFrameworkCore with support for reload on update the via the
query change notifications capability exposed by
Microsoft.Data.SqlClient.SqlDependency along with SQL Server's
Change Tracking feature.
Goals
- No polling!
- Updates happen in background via worker service (
IHostedService) - Only update settings which change rather than reloading all of them
Requirements
- .NET 8
Gotchas
- Won't work with Azure SQL until Microsoft adds/enables Service Broker support
- Setting values cannot be
null(as signified by theRequiredAttributeonISetting.Value)
Known Issues
- Not tested under load
Configuration Options
There is a single property which can be configured (cf. the SqlServerNotificationConfigurationReloaderOptions POCO)
ConnectionString- the full connection string for the SQL Server instance
Setup
For SqlServerNotificationConfigurationReloader to work it requires Change Tracking to be enable on the Settings table (and therefore also on the database itself)
eg:
ALTER DATABASE myDatabase
SET CHANGE_TRACKING = ON (AUTO_CLEANUP = ON);
ALTER TABLE dbo.Settings
ENABLE CHANGE_TRACKING;
Reccommend adding your SQL to the migration which adds the Settings table/view (or a new migration if that table/view already exists).
Usage Example
using RAIC.Extensions.Configuration.EntityFrameworkCore;
using RAIC.Extensions.Configuration.EntityFrameworkCore.Extensions;
using RAIC.Extensions.Configuration.EntityFrameworkCore.SqlServer.Extensions;
[Table("settings", Schema = "dbo")] // Need to explicitly set schema somehow, this is one way to do it
public record Setting : ISetting
{
[Key]
public required string Key { get; set; }
[Required]
public required string Value { get; set; }
}
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
{
public DbSet<Setting> Settings { get; set; }
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
}
var builder = Host.CreateApplicationBuilder(args); // or WebApplication.CreateBuilder(args);
// build an initial configuration
builder.Configuration.AddJsonFile("appsettings.json")
...
.AddUserSecrets<Program>(); // or wherever your connection string lives
builder.Configuration.AddDbSet<MyDbContext, Setting>(dbContextOptions => dbContextOptions.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
...
// Add the SqlServerNotificationConfigurationReloader background service and supporting services to obtain setting reloading functionalty
builder.Services.AddSqlServerNotificationConfigurationReloadService<MyDbContext, Settings>(); // uses connection string from MyDbContext. Other overrides exist if this doesn't work for you - see cods docs
await builder.Build().RunAsync(); // use config as normal
Read more about Configuration and Options on the Microsoft Docs site.