The pre-release RC1 version of FeatureToggle with .NET Core support is now available on NuGet.
See release notes and GitHub issues for additional background/breaking changes/limitations.
The main drive for v4 is to add initial .NET Core support.
Using Feature Toggle in a .NET Core Console App
In Visual Studio 2017, create a new .NET Core Console App and install the NuGet package. This will also install the dependent FeatureToggle.NetStandard package (.NET Standard 1.4).
Add the following code to Program.cs:
using System;
using FeatureToggle;
namespace ConsoleApp1
{
public class Printing : SimpleFeatureToggle { }
public class Saving : EnabledOnOrAfterDateFeatureToggle { }
public class Tweeting : EnabledOnOrAfterAssemblyVersionWhereToggleIsDefinedToggle { }
class Program
{
static void Main(string[] args)
{
var p = new Printing();
var s = new Saving();
var t = new Tweeting();
Console.WriteLine($"Printing is {(p.FeatureEnabled ? "on" : "off")}");
Console.WriteLine($"Saving is {(s.FeatureEnabled ? "on" : "off")}");
Console.WriteLine($"Tweeting is {(t.FeatureEnabled ? "on" : "off")}");
Console.ReadLine();
}
}
}
Running the application will result in an exception due to missing appSettings.config file: “System.IO.FileNotFoundException: 'The configuration file 'appSettings.json' was not found and is not optional.“ By default, FeatureToggle will expect toggles to be configured in this file, add an appSettings.json and set its Copy To Output Directory to “Copy if newer” and add the following content:
{
"FeatureToggle.Printing": "true",
"FeatureToggle.Saving": "01-Jan-2014 18:00:00",
"FeatureToggle.Tweeting": "2.5.0.1" // Assembly version is set to 2.5.0.0
}
Running the app now result in:
Printing is on
Saving is on
Tweeting is off
Using Feature Toggle in an ASP.NET Core App
Usage in an ASP.NET core app currently requires the configuration to be provided when instantiating a toggle, this may be cleaned up in future versions. For RC1 the following code shows the Startup class creating a FeatureToggle AppSettingsProviderand and passing it the IConfigurationRoot from the startup class.
public void ConfigureServices(IServiceCollection services)
{
// Set provider config so file is read from content root path
var provider = new AppSettingsProvider { Configuration = Configuration };
services.AddSingleton(new Printing { ToggleValueProvider = provider });
services.AddSingleton(new Saving { ToggleValueProvider = provider });
// Add framework services.
services.AddMvc();
}
The appSettings would look something like the following:
{
"FeatureToggle.Printing": "true",
"FeatureToggle.Saving": "false",
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
As an example of using this configuration check out the example project on GitHub, in particular the following:
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/Models/Printing.cs
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/Models/Saving.cs
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/ViewModels/HomeIndexViewModel.cs
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/Controllers/HomeController.cs
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/Views/Home/Index.cshtml
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/Startup.cs
https://github.com/jason-roberts/FeatureToggle/blob/master/src/Examples/AspDotNetCoreExample/appsettings.json
SHARE: