Table of Contents

Getting Started

📌 Introduction

Before you begin, here’s a quick preview of what the OpenSettings looks like after completing the steps below:

Demo

Once you’re ready, follow these steps to integrate OpenSettings into your project:

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 comparison.

🔹 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.MapControllers();
...

📌 Defining Your First Setting

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

using OpenSettings.Services.Interfaces;

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

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

Warning

If you later change the class name or namespace, the settings may no longer match existing data unless you specify the new name/namespace explicitly. Learn more about handling class name or namespace changes.


🔹 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 MyFirstSettings _MyFirstSettings;

    public MyService(MyFirstSettings MyFirstSettings)
    {
        _MyFirstSettings = MyFirstSettings;
    }
}

🔹 Using IOptions for Configuration Binding

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

using Microsoft.Extensions.Options;

public class MyService
{
    private readonly MyFirstSettings _MyFirstSettings;

    public MyService(IOptions<MyFirstSettings> options)
    {
        _MyFirstSettings = 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 (which, in our case, is null).

using OpenSettings;

var MyFirstSettings = SettingsProvider.GetLocalSettingOrDefault<MyFirstSettings>();

✅ What's Next?

🔹 Explore More Quick Start Guides

🔹 Deep Dive into OpenSettings


OpenSettings makes settings management simple and efficient! 🚀