Table of Contents

Getting Started

To integrate OpenSettings into your project, you'll first need to install the required libraries.

1️⃣ Install OpenSettings

dotnet add package OpenSettings.AspNetCore

2️⃣ Install a Storage Provider

For this guide, we'll use InMemory storage for simplicity. Install it with:

dotnet add package Microsoft.EntityFrameworkCore.InMemory

If you're using a different database, replace this package with the appropriate EF Core provider (e.g., Microsoft.EntityFrameworkCore.SqlServer for SQL Server).


🏗 Configuring OpenSettings

OpenSettings can run in two modes: Provider or Consumer. Choose the appropriate configuration based on your application’s role. For more information check out the Consumer vs Provider

🔹 Provider Configuration

var openSettingsConfiguration = new OpenSettingsConfiguration(ServiceType.Provider)
{
    Client = new ClientInfo(
        new Guid("adbdf741-bb4d-4673-b2a8-23e677fcf454"), // The unique identifier for the client.
        new Guid("4294a5e3-0839-4358-a03d-1ac52585ae5f")  // The secret key for the client.
    ),
};

🗄 Configuring Database Storage

// Configure database storage (InMemory for this example)
openSettingsProviderConfiguration.Provider.Orm.ConfigureDbContext = optsBuilder =>
{
    optsBuilder.UseInMemoryDatabase("OpenSettings");
};

🔹 Consumer Configuration

var openSettingsConfiguration = new OpenSettingsConfiguration(ServiceType.Consumer)
{
    Client = new ClientInfo(
        new Guid("71059bda-bb49-447f-ac83-60cd15c9518d"), // The unique identifier for the client.
        new Guid("6c52c9f7-d43c-44c1-8d6c-451bf9029731")  // The secret key for the client.
    ),
	Consumer = new ConsumerConfiguration
    {
      ProviderUrl = "http://localhost:5002/api/settings", // Url of the provider service.
    }
};

🔧 Registering OpenSettings

1️⃣ Enable OpenSettings In The Host Builder

await builder.Host.UseOpenSettingsAsync(openSettingsProviderConfiguration);

2️⃣ Add OpenSettings Controllers

builder.Services
    .AddControllers()
    .AddOpenSettingsController(builder.Configuration); // Enables OpenSettings Controllers

3️⃣ Integrate OpenSettings Middleware

Ensure OpenSettings is registered in the pipeline between UseRouting and MapControllers.

...
app.UseRouting();

app.UseOpenSettings(); // Updates instance status when the application starts or stops.
app.UseOpenSettingsSpa(); // Enables OpenSettings SPA for viewing & editing settings.

app.MapControllers();
...

📌 Defining Your First Setting

Define a setting by creating a new class that implements ISettings.

using OpenSettings.Services.Interfaces;

public class MyFirstSetting : ISettings
{
    public string Name { get; set; }
    public string Description { get; set; }
}

Run the application and navigate to ".../settings" to view and manage your settings.

Demo


🔹 Resolving Settings in the Application

You can inject and access your settings using dependency injection.

🔹 Accessing as a Singleton

Inject the setting directly into your constructor:

public class MyService
{
    private readonly MyFirstSetting _myFirstSetting;

    public MyService(MyFirstSetting myFirstSetting)
    {
        _myFirstSetting = myFirstSetting;
    }
}

🔹 Using IOptions for Configuration Binding

Alternatively, use IOptions<T> for accessing configuration-based settings:

using Microsoft.Extensions.Options;

public class MyService
{
    private readonly MyFirstSetting _myFirstSetting;

    public MyService(IOptions<MyFirstSetting> options)
    {
        _myFirstSetting = options.Value;
    }
}

This approach is useful when the settings are dynamically loaded from configuration sources.

Note

The default settings registration mode is Both, meaning it supports both Singleton and IOptions interfaces. You can change this behavior as needed.

🔹 Resolving Settings Without Dependency Injection

If you need to access settings outside of DI, use SettingsProvider.GetLocalSettingOrDefault<T>(). This method returns the setting if found or falls back to the default value if not available.

using OpenSettings;

var myFirstSetting = SettingsProvider.GetLocalSettingOrDefault<MyFirstSetting>();

✅ What's Next?

🔹 Explore More Quick Start Guides

🔹 Deep Dive into OpenSettings


OpenSettings makes settings management simple and efficient! 🚀