DEV Community

🔥 Azure App Configuration και χρήση του στην εφαρμογή .NET Core.

✅ Πώς λειτουργεί η δυναμική αλλαγή ρυθμίσεων (real-time configuration updates)
✅ Ανάλυση των IConfiguration, IOptions, IOptionsSnapshot, IOptionsMonitor
✅ Παράδειγμα χρήσης του RefreshAll στο Azure
✅ Πώς να κάνουμε αυτόματη ανανέωση των τιμών χωρίς restart


1️⃣ Business Scenario – Γιατί να χρησιμοποιήσω δυναμικές ρυθμίσεις;

Ας υποθέσουμε ότι έχουμε μια e-commerce εφαρμογή που προσφέρει εκπτώσεις στα προϊόντα.

Έχουμε την εξής ρύθμιση στο Azure App Configuration:

Key Value
DiscountPercentage 10

Χωρίς δυναμική ανανέωση:

  • Αν αλλάξουμε το DiscountPercentage στο Azure, πρέπει να κάνουμε restart την εφαρμογή.

Με δυναμική ανανέωση:

  • Η αλλαγή εφαρμόζεται αυτόματα σε πραγματικό χρόνο!

Παράδειγμα:

  1. Ο admin αλλάζει το DiscountPercentage από 10 σε 15.
  2. Όλοι οι χρήστες βλέπουν 15% έκπτωση αντί για 10% χωρίς restart! 🚀

2️⃣ Σύνδεση του .NET Core με το Azure App Configuration

Βήμα 1: Προσθήκη του NuGet Package

dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore


Βήμα 2: Φόρτωση Ρυθμίσεων στο Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options.Connect("Endpoint=https://myappconfig.azconfig.io;Id=XXXXX;Secret=YYYYY")
           .UseFeatureFlags()
           .Select("*", LabelFilter.Null)
           .Watch("DiscountPercentage", TimeSpan.FromSeconds(10)); // Refresh κάθε 10 δευτερόλεπτα
});

builder.Services.AddAzureAppConfiguration();

var app = builder.Build();
app.UseAzureAppConfiguration();
app.Run();
Enter fullscreen mode Exit fullscreen mode

🔍 Ανάλυση:

  • Συνδέεται με το Azure App Configuration
  • Φορτώνει τις ρυθμίσεις στην εφαρμογή
  • Ελέγχει για αλλαγές κάθε 10 δευτερόλεπτα

3️⃣ Σύγκριση των IConfiguration, IOptions, IOptionsSnapshot, IOptionsMonitor

🟢 IConfiguration

Το IConfiguration είναι το βασικό interface για να διαβάζουμε ρυθμίσεις.

public class DiscountService
{
    private readonly IConfiguration _configuration;

    public DiscountService(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void ShowDiscount()
    {
        string discount = _configuration["DiscountPercentage"];
        Console.WriteLine($"Current Discount: {discount}%");
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Μειονέκτημα: Δεν υποστηρίζει δυναμική ανανέωση.


🟡 IOptions

Το IOptions παρέχει strongly-typed settings.
Βήμα 1: Δημιουργία Μοντέλου

public class DiscountSettings
{
    public int DiscountPercentage { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Βήμα 2: Δήλωση στο Program.cs

builder.Services.Configure<DiscountSettings>(builder.Configuration.GetSection("DiscountSettings"));
Enter fullscreen mode Exit fullscreen mode

Βήμα 3: Χρήση του IOptions

public class DiscountService
{
    private readonly DiscountSettings _settings;

    public DiscountService(IOptions<DiscountSettings> options)
    {
        _settings = options.Value;
    }

    public void ShowDiscount()
    {
        Console.WriteLine($"Current Discount: {_settings.DiscountPercentage}%");
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Μειονέκτημα: Οι ρυθμίσεις δεν ανανεώνονται δυναμικά χωρίς restart.


🟠 IOptionsSnapshot

Το IOptionsSnapshot ανανεώνεται σε κάθε HTTP Request. Χρησιμοποιείται σε Scoped Services.

public class DiscountService
{
    private readonly DiscountSettings _settings;

    public DiscountService(IOptionsSnapshot<DiscountSettings> options)
    {
        _settings = options.Value;
    }

    public void ShowDiscount()
    {
        Console.WriteLine($"Current Discount: {_settings.DiscountPercentage}%");
    }
}
Enter fullscreen mode Exit fullscreen mode

🚀 Διαφορά από IOptions:

  • Το IOptionsSnapshot ανανεώνεται σε κάθε HTTP Request.

- Είναι χρήσιμο αν θέλουμε dynamic reload σε Scoped services.

🔵 IOptionsMonitor

Το IOptionsMonitor είναι το μόνο που υποστηρίζει πλήρη δυναμική ανανέωση σε Singleton services.

public class DiscountService
{
    private readonly IOptionsMonitor<DiscountSettings> _optionsMonitor;

    public DiscountService(IOptionsMonitor<DiscountSettings> optionsMonitor)
    {
        _optionsMonitor = optionsMonitor;
    }

    public void ShowDiscount()
    {
        var settings = _optionsMonitor.CurrentValue;
        Console.WriteLine($"Current Discount: {settings.DiscountPercentage}%");
    }
}
Enter fullscreen mode Exit fullscreen mode

✅ Πλεονέκτημα: Η τιμή αλλάζει δυναμικά χωρίς restart!


4️⃣ Χρήση Event για Auto-Refresh

Μπορούμε να εκτελούμε κώδικα κάθε φορά που αλλάζει μία ρύθμιση στο Azure:

public class DiscountService
{
    public DiscountService(IOptionsMonitor<DiscountSettings> optionsMonitor)
    {
        optionsMonitor.OnChange(settings =>
        {
            Console.WriteLine($"New Discount: {settings.DiscountPercentage}%");
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

5️⃣ Χρήση του RefreshAll στο Azure App Configuration

Το RefreshAll είναι ένας μηχανισμός που μας επιτρέπει να ανανεώνουμε όλες τις ρυθμίσεις όταν αλλάζει μία τιμή.

Σενάριο:

➡️ Αν η ρύθμιση RefreshAll αυξηθεί κατά 1, τότε η εφαρμογή κάνει force reload όλων των ρυθμίσεων.

Προσθήκη στο Program.cs

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options.Connect("Endpoint=https://myappconfig.azconfig.io")
           .Watch("RefreshAll", TimeSpan.FromSeconds(5)); // Refresh κάθε 5 δευτερόλεπτα
});
Enter fullscreen mode Exit fullscreen mode

Χρήση στο Service

public class RefreshService
{
    private readonly IOptionsMonitor<DiscountSettings> _optionsMonitor;

    public RefreshService(IOptionsMonitor<DiscountSettings> optionsMonitor)
    {
        _optionsMonitor = optionsMonitor;
        _optionsMonitor.OnChange(settings =>
        {
            Console.WriteLine($"Settings Refreshed: New Discount is {settings.DiscountPercentage}%");
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

🔥 Τι συμβαίνει;
Αν αλλάξουμε το RefreshAll, όλες οι ρυθμίσεις ανανεώνονται αυτόματα!


6️⃣ Συμπέρασμα

🚀 Αν θέλεις πλήρη δυναμική ανανέωση, χρησιμοποίησε IOptionsMonitor και το RefreshAll στο Azure App Configuration! 🎯

  1. Χρήση του Azure App Configuration για δυναμική ανανέωση ρυθμίσεων στο ASP.NET Core:
    Tutorial: Χρήση δυναμικής διαμόρφωσης σε εφαρμογή ASP.NET Core

  2. Κατανόηση και χρήση των IOptions, IOptionsSnapshot και IOptionsMonitor στο ASP.NET Core:
    Πρότυπο Options στο ASP.NET Core

  3. Διαφορές μεταξύ IOptions, IOptionsSnapshot και IOptionsMonitor:
    Διαφορά μεταξύ IOptions, IOptionsSnapshot και IOptionsMonitor στο ASP.NET Core

  4. Δυναμική ανανέωση ρυθμίσεων από το Azure App Configuration στο .NET Core:
    Δυναμική ανανέωση ρυθμίσεων στο .NET Core από το Azure App Configuration

  5. Χρήση του IOptionsSnapshot για ανάγνωση ενημερωμένων δεδομένων:
    Πρότυπο Options στο ASP.NET Core

  6. Οδηγός για τη χρήση του Azure App Configuration με εφαρμογές ASP.NET Core:
    Γρήγορη εκκίνηση για το Azure App Configuration με εφαρμογή ASP.NET Core

Top comments (0)