0

My problem is that I'm trying to pass to the constructor of a custom service some configurations keys that reside in my appsettings.json at the startup time. I want to know how can I achieve this.

This is my setup:

This is the interface for the email service

public interface IEmailService
{
   Task<bool> SendEmailAsync(string email, string subject, string message);
}

This is the interface for the parameters that are in the config file

public interface IAWSCredentialsConfig
{
    string AccessKeyId { get; }
    string SecretAccessKey { get; }
}

An this is the constructor of the implementation of the email service:

public EmailService(IAWSCredentialsConfig config)
{
      _config = config;

      ClientConfig = new AmazonSimpleEmailServiceConfig
      {
           RegionEndpoint = RegionEndpoint.USEast1
      };
}

How can I inject those two parameters into a Singleton instance of the EmailService?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Juanma
  • 905
  • 2
  • 11
  • 25
  • 1
    read up here. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration#use-options-and-configuration-objects – Nkosi Nov 21 '17 at 03:28
  • 1
    look specifically at the `IOptions<>` change your config to a class and use the `IOption` to inject it into your dependent class – Nkosi Nov 21 '17 at 03:29
  • 1
    Register your configuration as service with: `services.Configure(Configuration.GetSection("YourSection"))` and pass it to your class constructor with `IOption`. – J.Hpour Nov 21 '17 at 12:48

0 Answers0