title: OptionsModel dependency injection in vNext console application
body: <p>I have a <strong>vNext console application</strong> where one of my classes takes the <code>OptionsModel&lt;T&gt;</code> POCO configuration settings class.</p>
  
  <p>I am unable to get the POCO settings class resolved and injected into my <code>RabbitMqConnection</code> class below.</p>
  
  <p>Setting up the <code>ServiceProvider</code> is not the issue, it's the resolution of the settings class.</p>
  
  <p>Note, this is a <strong>vNext console application</strong> (not an mvc6 app).</p>
  
  <p>My second question is, and I understand constructor arguments should be kept minimal, but is it not best to pass the two strings as constructor arguments rather than an <code>IOptions</code> class as the former method is more descriptive of what the <code>RabbitMqConnection</code> class requires? If so, how is this best injected from the class that defined the mappings (<code>Program.cs</code> file in example below)</p>
  
  <pre><code>public class RabbitMqConnection
  {
      public string HostName { get; set; }
      public string UserName { get; set; }
  
      public RabbitMqConnection(IOptions&lt;MessagingSettings&gt; settings)
      {
          HostName = settings.Value.HostName;
          UserName = settings.Value.UserName;
      }
  }
  
  public class MessagingSettings
  {
      public string HostName { get; set; }
      public string UserName { get; set; }
  }
  
  appsettings.json
  
  {
      "MessagingSettings":{
          "HostName":"localhost",
          "Username":"guest"
      }
  }
  
  public void ConfigureServices(IServiceCollection services)
  {
      // tried registration a number of ways as below
      services.Configure&lt;MessagingSettings&gt;(Configuration.GetSection("MessagingSettings"));
      services.Configure&lt;MessagingSettings&gt;(Configuration);
  
      // attempt 1 - get runtime error saying cant resolve IOptions&lt;MessageSettings&gt;
      services.TryAdd(ServiceDescriptor.Singleton&lt;RabbitMqConnection, RabbitMqConnection&gt;());
  
      // attempt 2 - same as above, when i breakpoint on messagingSettings, all the values in the object are null
      services.TryAdd(ServiceDescriptor.Singleton&lt;RabbitMqConnection&gt;(factory =&gt;
      {
          // instead of injecting the MessageSettings, pass through the string values (constructor omitted for clarity)
          var messagingSettings = Configuration.Get&lt;MessagingSettings&gt;();
          return new RabbitMqConnection(messagingSettings.HostName, messagingSettings.UserName);
      }));
  }
  
  var conn = ServiceProvider.GetRequiredService&lt;RabbitMqConnection&gt;();
  </code></pre>