title: Accessing settings from appsettings.json from class library in Core 2.0
body: <p>I've read a dozen questions here on SO and at least 4 blog posts but I cannot get this to work in a VS2017 solution with multiple class library projects. However, I <em>can</em> get it to work with a solution with a web application and one class library, albeit with a different pattern than the one I've found in the documentation and here on SO.</p>
  
  <p>My solution contains 5 projects a WebApplication, Testing, Infrastructure, Services, Domain (the latter three are class libraries).</p>
  
  <p>In the infrastructure project I have created a class called EmailConfigurationSettings and registered it in the StartUp class of the WebApp.</p>
  
  <p>This class is in class library:</p>
  
  <pre><code>public class EmailConfigurationSettings
  {
      public string Provider { get; set; }
      public string Username { get; set; }
      public string Password { get; set; }
  }
  </code></pre>
  
  <p>This is in StartUp.cs in WebApp:</p>
  
  <pre><code>services.Configure&lt;EmailConfigurationSettings&gt;(Configuration.GetSection("EmailConfigurationSettings"));
  services.AddOptions();
  </code></pre>
  
  <p>appsettings.json</p>
  
  <pre><code>"EmailConfigurationSettings": {
    "Provider": "gmail",
    "Username": "user@gmail.com",
    "Password": "mypassword" 
  } 
  </code></pre>
  
  <p>I need the settings in the Services project. But for now I am just trying to ensure I can actually retrieve them. The code below follows all the code samples I found but generates:</p>
  
  <pre><code>public class LoadEmailSettingsFromAppSettings
  {
      private readonly IOptions&lt;EmailConfigurationSettings&gt; _emailSettings;
  
      public LoadEmailSettingsFromAppSettings(IOptions&lt;EmailConfigurationSettings&gt; emailSettings)
      {
          _emailSettings = emailSettings;
          Message = "Our provider is " + _emailSettings.Value.Provider;
      }
  
      public string Message { get; set; }
  }
  </code></pre>
  
  <p>_emailSettings is always null</p>
  
  <p>So I tried testing it in the TestProject:</p>
  
  <pre><code>IOptions&lt;EmailConfigurationSettings&gt; someOptions = Options.Create&lt;EmailConfigurationSettings&gt;(new EmailConfigurationSettings());
  </code></pre>
  
  <p>Well, that's null too.</p>
  
  <p>In the other solution, the one with the WebApp and one class library,
  using a slightly different pattern I found on one of the blogs I can actually retrieve the values from appsettings.json:</p>
  
  <pre><code>public class AuthMessageSender : IEmailSender, ISmsSender
  {
      public AuthMessageSender(IOptions&lt;EmailConfig&gt; emailSettings)
      {
          _emailSettings = emailSettings.Value;
      }
  
      public EmailConfig _emailSettings { get; }
  
      public Task SendEmailAsync(string email, string subject, string message)
      {
          Execute(email, subject, message).Wait();
          return Task.FromResult(0);
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6gmhH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6gmhH.png" alt="Class populated from appsettings.json"></a></p>
  
  <p>Please note the IOptions class was different with 9 properties but constructed the same way as the pattern above.</p>
  
  <p>But using this pattern does <em>not</em> work in the multiple class library project. How can I understand how to get this to work in a multiple class library scenario?</p>
  
  <p>Why they abandoned the ConfigurationManager I don't know, it was far easier.</p>