stackoverflow - NFig.AspNetCore 0.2.11
Package Description
PM> Install-Package NFig.AspNetCore -Version 0.2.11 -Source https://www.myget.org/F/stackoverflow/api/v3/index.json
> nuget.exe install NFig.AspNetCore -Version 0.2.11 -Source https://www.myget.org/F/stackoverflow/api/v3/index.json
> dotnet add package NFig.AspNetCore --version 0.2.11 --source https://www.myget.org/F/stackoverflow/api/v3/index.json
source https://www.myget.org/F/stackoverflow/api/v3/index.json
nuget NFig.AspNetCore ~> 0.2.11
Copy to clipboard
> choco install NFig.AspNetCore --version 0.2.11 --source https://www.myget.org/F/stackoverflow/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "stackoverflow" -SourceLocation "https://www.myget.org/F/stackoverflow/api/v2"
Install-Module -Name "NFig.AspNetCore" -RequiredVersion "0.2.11" -Repository "stackoverflow"
Copy to clipboard
NFig.AspNetCore
Provides ASP.NET Core support for NFig by integrating into the options framework.
Package Status
MyGet Pre-release feed: https://www.myget.org/gallery/stackoverflow
Package | NuGet Stable | NuGet Pre-release | Downloads | MyGet |
---|---|---|---|---|
NFig.AspNetCore |
Getting Started
Integrating NFig into your ASP.NET Core application is easy!
Installation
Install the [NFig.AspNetCore](https://www.nuget.org/packages/NFig.AspNetCore/)
NuGet package using:
dotnet add <project> package NFig.AspNetCore
If setting up a web project it's a good idea to check out the sample project.
Configuration
NFig can be configured by using the code below. You can see we have defined a Settings
class containing the settings our application needs, a Tier
enum representing the different tiers our application can run in and a DataCenter
enum representing the data centers that our application runs in.
We add a call to AddNFig
in our ConfigureServices
method specifying the types above so NFig knows how to manage our settings. Under the hood this tells the DI container how to resolve an IOptions<Settings>
and also IOptions<T>
where T
is any setting group specified within our settings (e.g. FeatureFlagSettings
).
Next we call UseNFig
in our Configure
method. This is passed a builder that can be used to configure NFig's backing store. Right now NFig only has Redis as a backing store so we simply configure Redis using a connection string from our configuration file. Our configuration file also tells us what tier and data center our application is running in, so we pass those to NFig as well.
That's it!
appsettings.json
{
"ApplicationName": "NFig Example",
"Tier": "Local",
"DataCenter": "Local",
"ConnectionStrings": {
"Redis": "localhost:6379,defaultDatabase=0"
}
}
Startup.cs
public class Settings
{
[SettingsGroup]
public FeatureFlagSettings FeatureFlags { get; private set; }
public class FeatureFlagSettings
{
[Description("Enables the [Whizzbang](https://trello.com/.../) feature.")]
[Setting(false)]
[TieredDefaultValue(Tier.Local, true)]
[TieredDefaultValue(Tier.Dev, true)]
public bool EnableWhizzbang { get; private set; }
}
}
public enum Tier
{
Local,
Dev,
Test,
Prod,
}
public enum DataCenter
{
Local,
Timbuktu,
}
public class AppSettings
{
public string ApplicationName { get; set; }
public Tier Tier { get; set; }
public DataCenter { get; set; }
}
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// add services needed by NFig
// under the hood this configures the DI container
// to resolve NFig settings as IOptions implementations
services.AddNFig<Settings, Tier, DataCenter>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// configure NFig's backing store
app
.UseNFig<Settings, Tier, DataCenter>(
(cfg, builder) =>
{
var settings = cfg.Get<AppSettings>();
var connectionString = cfg.GetConnectionString("Redis");
// connects NFig to Redis
builder.UseRedis(settings.ApplicationName, settings.Tier, settings.DataCenter, connectionString);
}
);
}
}
Exposing NFig UI
NFig.AspNetCore contains middleware that can render NFig.UI. It can be called from within MVC or by a route handler in Kestrel. This way you can lock down the route using your current security models.
Using MVC
public class SettingsController
{
// restrict this route to users in the Admins role
[Authorize(Roles = "Admins")]
[Route("settings/{*pathInfo}")]
public Task Settings() => NFigMiddleware<Settings, Tier, DataCenter>.HandleRequestAsync(HttpContext);
}
Using Kestrel
public class Startup
{
// ... other startup bits here
public void Configure(IAppBuilder app, IHostingEnvironment env)
{
app.MapWhen(
ctx => ctx.Request.Path.StartsWithSegments("/settings"),
appBuilder => appBuilder.Run(NFigMiddleware<Settings, Tier, DataCenter>.HandleRequestAsync)
);
}
}
Using Our Settings
To use our settings all we have to do is inject them wherever we need to use them.
In an MVC Controller
public class HomeController : Controller
{
private readonly FeatureFlagSettings _featureFlags;
public HomeController(IOptions<FeatureFlagSettings> featureFlags)
{
_featureFlags = featureFlags.Value;
}
public IActionResult Home()
{
if (_featureFlags.EnableWhizzbang)
{
return Content("Whizzbang enabled!");
}
return Content("Whizzbang disabled");
}
}
In an MVC View
@model MyModel
@inject IOptions<FeatureFlagSettings> featureFlags
@if (featureFlags.Value.EnableWhizzbangFeature)
{
<strong>Whizzbang enabled</strong>
}
else
{
<strong>Whizzbang disabled</strong>
}
Futures
- support NFig 3.x
- some tests would be nice
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration (>= 3.1.6)
- Microsoft.Extensions.Configuration.Abstractions (>= 3.1.6)
- Microsoft.Extensions.Options (>= 3.1.6)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 3.1.6)
- NFig.Redis (>= 2.2.1)
- NFig.UI (>= 3.0.3)
- .NETCoreApp 3.1: 3.1.0.0
OwnersNick Craver deanward81 |
AuthorsStack Exchange, Inc., deanward81 |
Project URLhttps://github.com/NFig/NFig.AspNetCore/ |
LicenseMIT |
Info20 total downloads |
2 downloads for version 0.2.11 |
Download (23.97 KB) |
Found on the current feed only |
Package history
Version | Size | Last updated | Downloads | Mirrored? | |||
---|---|---|---|---|---|---|---|
0.2.15 | 24.05 KB | Tue, 28 Jul 2020 16:01:38 GMT | 3 | ||||
0.2.11 | 23.97 KB | Sun, 26 Jul 2020 20:23:18 GMT | 2 | ||||
0.2.10 | 23.98 KB | Sun, 26 Jul 2020 20:19:56 GMT | 2 | ||||
0.2.9 | 23.97 KB | Sun, 26 Jul 2020 20:17:26 GMT | 2 | ||||
0.2.8 | 23.97 KB | Sun, 26 Jul 2020 20:04:32 GMT | 1 | ||||
0.2.6 | 15.47 KB | Wed, 16 Jan 2019 22:51:38 GMT | 4 | ||||
0.2.5 | 15.47 KB | Wed, 16 Jan 2019 22:48:13 GMT | 2 | ||||
0.2.4 | 15.47 KB | Wed, 16 Jan 2019 22:43:45 GMT | 2 | ||||
0.2.3 | 15.23 KB | Wed, 16 Jan 2019 22:26:45 GMT | 2 |