Structr
  • Welcome
  • Utilities
    • Abstractions
      • Check
      • Ensure
      • Money
      • HierarchyId
      • Providers
        • SequentialGuidProvider
      • Extensions
        • DateTime
        • Dictionary
        • DirectoryInfo
        • Enumerable
        • Enum
        • Expression
        • Int
        • Long
        • MemberInfo
        • Object
        • Queryable
        • ServiceCollection
        • String
        • Type
      • Helpers
        • AsyncHelper
        • BindHelper
      • JsonConverters
        • DateOnly
        • TimeOnly
        • StringNumber
    • Collections
      • AutoMapper extensions
    • IO
      • FileHelper
      • MimeTypeHelper
      • PathHelper
      • SequentialFileName
    • Configuration
      • Providers
        • JSON-file
        • XML-file
        • In-Memory
        • Consul
      • Get settings
      • Set settings
      • Customization
    • Email
      • Razor
    • Navigation
      • Menu
      • Breadcrumbs
    • Security
  • Domain
    • Domain
      • Entities
      • Value objects
  • Data Access
    • Entity Framework Core
    • Entity Framework 6
  • Use Cases
    • Operations
      • Filtering
      • Decoration
    • Notices
    • Validation
    • Specifications
    • Stateflows
      • StateMachine
      • Configurations
  • Presentation
    • ASP.NET Core
      • Client
      • Http
      • JavaScript
      • Json
      • Mvc
      • Referrer
      • Rewrite
      • Routing
      • TagHelpers
      • Validation
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Utilities
  2. Configuration

Get settings

IConfiguration<> is the main service to read settings.

Example: Inject IConfiguration<> into a application service:

public class EmailService
{
    private readonly SmtpEmailSettings _settings;

    public NotificationService(IConfiguration<SmtpEmailSettings> configuration)
        => _settings = configuration.Settings;

    public Task SendEmailAsync(string email, string subject, string message)
    {
        using (var smtpClient = new SmtpClient(_settings.Host, _settings.Port))
        {
            /* Do send logic here */
        }
    }
}

Important thing that, in this example if you register EmailService as scoped, then private readonly field _settings that contains SmtpEmailSettings will not modified even origin SmtpEmailSettings will be changed. This will happen because you get settings SmtpEmailSettings from IConfiguration<SmtpEmailSettings> in the constructor.

So if you need get actual settings realtime, you should preserve IConfiguration<SmtpEmailSettings> in application service:

public class EmailService
{
    private readonly IConfiguration<SmtpEmailSettings> _configuration;

    public EmailService(IConfiguration<SmtpEmailSettings> configuration)
        => _configuration = configuration;

    public Task SendEmailAsync(string email, string subject, string message)
    {
        var settings = _configuration.Settings; // Now, it is actual settings every `SendEmailAsync` invoke
        using (var smtpClient = new SmtpClient(settings.Host, settings.Port))
        {
            /* Do send logic here */
        }
    }
}
PreviousConsulNextSet settings

Last updated 2 years ago

Was this helpful?