Getting started with Esquio in .NET Core

In this article, we are going to see how easy it is to use Esquio in your .NET Core application using the NuGet packages provided by Xabaril.

> In samples/ConsoleApp you’ll find a complete Esquio example in ASP.NET Core.

Create a folder for your new project

Open a command prompt an run:

mkdir consoleapp
cd consoleapp

Create the project

To create the project type the following command using the .NET Core CLI:

dotnet new console

Installation

To install Esquio type the following command:

dotnet package add Microsoft.Extensions.DependencyInjection
dotnet package add Microsoft.Extensions.Logging.Console
dotnet package add Microsoft.Extensions.Configuration.Json
dotnet package add Esquio.Configuration.Store
dotnet restore

or using Powershell or Package Manager:

Install-Package Microsoft.Extensions.DependencyInjection
Install-Package Microsoft.Extensions.Logging.Console
Install-Package Microsoft.Extensions.Configuration.Json
Install-Package Esquio.Configuration.Store

Setup

In the Program.cs change the Main method to async:

static async Task Main(string[] args)

Import the configuration namespace:

using Microsoft.Extensions.Configuration;

Create the configuration object:

var configuration = new ConfigurationBuilder()
  .SetBasePath(Directory.GetCurrentDirectory())
  .AddJsonFile("appsettings.json")
  .Build();

Add a appsettings.json file to the project and add the content below:

{
  "Esquio": {
    "Products": [
      {
        "Name": "Console",
        "Features": [
          {
            "Name": "Colored",
            "Enabled": true,
            "Toggles": []
          }
        ]
      }
    ]
  }
}

Register the Esquio services and the configuration store:

var services = new ServiceCollection()
  .AddLogging(configure => configure.AddConsole())
  .AddEsquio()
  .AddConfigurationStore(configuration, "Esquio")
  .Services;

Build the service provider:

var serviceProvider = services.BuildServiceProvider();

Obtain a context for the IFeatureService:

var featureService = serviceProvider.GetService<IFeatureService>();

Check if “Colored” feature for the “Console” project is enabled and set the background color of the console to blue:

if (await featureService.IsEnabledAsync("Colored", "Console"))
{
  Console.BackgroundColor = ConsoleColor.Blue;
}

Finally write a message to check it:

Console.WriteLine("Welcome to Esquio!");
Console.Read();

Test the application

Test the app from your machine running:

dotnet run
../_images/consoleapp.png

To disable the feature, change the appsettings.json:

"Enabled": false

Test again the app:

../_images/consoleapp2.png