title: ASP.NET Core 3.1 injecting array of objects from appsettings.json into an injected service
body: <p>I'm trying to inject a couple email accounts that I have in appsettings.json into an email service.</p>
  
  <p><strong>EDIT:</strong> My <code>EmailRepository.cs</code> needs a <code>DbContext</code>injected as well. I used @Nkosi 's answer, which worked without needing a DbContext. I'm planning on using <code>DbContextPool</code> in production, so how do I pull one of those out in my <code>ConfigureServices</code> method?</p>
  
  <p>appsettings.json:</p>
  
  <pre><code>"SanSenders": [
    {
      "Host": "mail.theFourSeasons.com",
      "FromName": "The Four Seasons",
      "IsNoReply": true,
      "IsSssl": true,
      "Password": "tooGoodToBeTrue",
      "Port": 465,
      "Username": "noreply@theFourSeasons.com"
    },
  
    {
      "Host": "mail.theFourSeasons.com",
      "FromName": "Franki",
      "IsNoReply": false,
      "IsSssl": true,
      "Password": "cantTakeMyEyesOffYou",
      "Port": 465,
      "Username": "franki@theFourSeasons.com"
    }
  ]
  </code></pre>
  
  <p>SanSender.cs:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class SanSender
  {
      public string FromName { get; set; }
      public string Host { get; set; }
      public bool IsNoReply { get; set; }
      public bool IsSsl { get; set; }
      public string Password { get; set; }
      public int Port { get; set; }
      public string Username { get; set; }
  
      public async Task&lt;bool&gt; SendEmailAsync(
          string toAddress, string subject, string htmlMessage)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>EmailRepository.cs:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class EmailRepository
  {
      public IEnumerable&lt;SanSender&gt; SanSenders { get; set; }
  
      //Edit: I need a DbContext injected as well.
      public EmailRepository(ApplicationDbContext applicationDbContext,  
          IEnumerable&lt;SanSender&gt; sanSenders)
      {
           SanSenders = sanSenders;
      }
  }
  </code></pre>
  
  <p>Startup.cs:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      services.Configure&lt;List&lt;SanSender&gt;&gt;((settings) =&gt;
      {
          Configuration.GetSection("SanSenders").Bind(settings);
      });
  
      services.AddScoped&lt;EmailRepository&gt;();
  
      services.AddControllersWithViews();
  }
  </code></pre>
  
  <p>The Controller:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class HomeController : Controller
  {
      public HomeController(
          IOptions&lt;List&lt;SanSender&gt;&gt; optionsSanSenders, EmailRepository emailService)
      {
      }
  
      public IActionResult Index()
      {
          return View();
      }
  }
  </code></pre>
  
  <p>In the controller I added <code>IOptions&lt;List&lt;SanSender&gt;&gt; optionsSanSenders</code> and I can get access to them there, but <code>EmailService.cs</code> is in a class library and I would prefer to not add unnecessary dependencies to it.</p>
  
  <p>The EmailService does have <code>IEnumerable&lt;SanSender&gt; SanSenders</code>, but it has a zero length/count.</p>
  
  <p>What am I doing wrong?</p>