title: Single object instance using IServiceCollection.AddSingleton()
body: <p>Consider the following simplistic appsettings.json:</p>
  
  <pre><code>{
    "maintenanceMode": true
  }
  </code></pre>
  
  <p>It is loaded inside my Startup.cs / Configure(...) method</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
  
      // Load appsettings.json config
      var builder = new ConfigurationBuilder()
          .SetBasePath(env.ContentRootPath)
          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
      _configuration = builder.Build();
  
      // Apply static dev / production features
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
      }
      else
      {
          app.UseHsts();
      }
  
      // Other features / settings
      app.UseHttpsRedirection();
      app.UseMvc();
  }
  </code></pre>
  
  <p>The _configuration is private inside the Startup.cs, it is used to deserialize the content into a structured model <strong>that will provide additonal functionality across the whole webservice lifetime</strong>:</p>
  
  <pre><code>// This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services) {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
      services.AddOptions();
  
      var runtimeServices = _configuration.Get&lt;RuntimeServices&gt;();
  
      services.AddSingleton(runtimeServices);
  }
  </code></pre>
  
  <p>The model looks like this:</p>
  
  <pre><code>public class RuntimeServices {
  
      [JsonProperty(PropertyName = "maintenanceMode")]
      public bool MaintenanceMode { get; set; }
  
  }
  </code></pre>
  
  <p>Controllers look like this:</p>
  
  <pre><code>[ApiController]
  public class ApplicationController : Base.Controller {
  
      private readonly RuntimeServices _services;
  
      public ApplicationController(IOptions&lt;RuntimeServices&gt; services) : base(services) {
          _services = services.Value;
      }
  
      // Web-api following ...
  
  }
  </code></pre>
  
  <p><strong>Now here comes the problem:</strong></p>
  
  <p>Upon startup right after the appsettings.json has been loaded and deserialized, the RuntimeServices instance holds all the correct information (yes, some of them have been omitted here).</p>
  
  <p>Hashcode inside Startup.cs / ConfigureServices():
  <a href="https://i.stack.imgur.com/BRH0M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BRH0M.png" alt="enter image description here"></a></p>
  
  <p>Hashcode inside any controller / api-call:
  <a href="https://i.stack.imgur.com/pgnk8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pgnk8.png" alt="enter image description here"></a></p>
  
  <p>The <code>GetHashCode()</code> method has not been tampered with.
  This results in the configuration originating form appsettings.json not being applied inside controller/api calls, all properties are instanciated with their default values / null.</p>
  
  <p>I was hoping using the <code>AddSingleton()</code> method would inject the <strong>very same</strong> instance and reuse it across the app lifetime. Can someone tell me why a new instance of RuntimeServices is being created? And how would I archive my goal in having a <strong>usable instance</strong> of my object inside Startup.cs and still access <strong>the same object instance</strong> inside my controllers?</p>
  
  <p>My go-to solution would be the usual singleton pattern. But I was hoping to solve this using the built-in functionality asp.net core provides.</p>