title: Unable to resolve service for type IOptions[DataAccessConfiguration] in non-ASP.NET Core app
body: <p>All of our business services were previously set up to use Dependency Injection with IOptions because they were being consumed by ASP.NET Core apps, like so:</p>
  
  <p>NotificationDataAccess.cs:</p>
  
  <pre><code>public class NotificationDataAccess : BaseDataAccess, INotificationDac&lt;Notification&gt;
      {
          public NotificationDataAccess(IOptions&lt;DataAccessConfiguration&gt; options, IClaimsAccessor claimsAccessor) :
              base(options, claimsAccessor)
          {
          }
  }
  </code></pre>
  
  <p>NotificationBusinessService.cs:</p>
  
  <pre><code>public class NotificationBusinessServices : INotificationServices&lt;Notification&gt;
      {
          private readonly INotificationDac&lt;Notification&gt; _notificationDataAccess;
  
          public NotificationBusinessServices(
              INotificationDac&lt;Notification&gt; notifcationDataAccess)
          {
              _notificationDataAccess = notifcationDataAccess;
          }
  }
  </code></pre>
  
  <p>Now I'm left with the unenviable task of trying to figure out how to leverage the same pattern from a windows service, which doesn't benefit from the built-in ASP.NET Core features for handling DI. When the service starts up, I execute the following code:</p>
  
  <pre><code>// Set up configuration, services, and logging.
              IServiceCollection services = new ServiceCollection();
              var startup = new Startup();
              startup.ConfigureServices(services);
              IServiceProvider serviceProvider = services.BuildServiceProvider();
  
              var configuration = serviceProvider.GetService&lt;IConfigurationRoot&gt;();
              var notificationService = serviceProvider.GetService&lt;INotificationServices&lt;Notification&gt;&gt;();// TODO: This errors!
              processor = new Processor(configuration, notificationService);
  </code></pre>
  
  <p>And here is the Startup.cs code, which is supposed to configure the services:</p>
  
  <pre><code>public class Startup
      {
          IConfigurationRoot Configuration { get; }
  
          public Startup()
          {
              var builder = new ConfigurationBuilder()
                  .SetBasePath(Path.Combine(AppContext.BaseDirectory))
                  .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                  .AddEnvironmentVariables();
              Configuration = builder.Build();
          }
  
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddSingleton&lt;IConfigurationRoot&gt;(Configuration);
              //services.AddMvc();
  
              // Add application services.
              Listings.Business.Configuration.Instance = new BusinessLayerConfiguration();
              services.Configure&lt;DataAccessConfiguration&gt;(options =&gt; Configuration.GetSection("Data").Bind(options));
              services.AddScoped(typeof(INotificationDac&lt;Notification&gt;), typeof(NotificationDataAccess));
              services.AddScoped(typeof(INotificationServices&lt;Notification&gt;), typeof(NotificationBusinessServices));
          }
      }
  </code></pre>
  
  <p>Unfortunately, when I run the windows service it throws an exception when trying to get the notificationService:</p>
  
  <pre><code>var notificationService = serviceProvider.GetService&lt;INotificationServices&lt;Notification&gt;&gt;();
  </code></pre>
  
  <p>The exception is:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'Unable to resolve service for type
    'Microsoft.Extensions.Options.IOptions`1[Rpr.Listings.DataAccess.DataAccessConfiguration]'
    while attempting to activate
    'Rpr.Listings.DataAccess.NotificationDataAccess'.'</p>
  </blockquote>
  
  <p>I was hoping my "services.Configure" code would resolve this, but alas no. Clearly I need to register IOptions in my Startup.cs, however I have no idea how to do so. Is this something that usually happens out of the box with ASP.NET MVC? Does "services.AddMvc();" normally register this binding correctly? I can call that, but would need to import a ton of ASP.NET MVC packages into my windows service, which I'm reluctant to do.</p>
  
  <p>Please let me know how to register the IOptions binding correctly, thanks!</p>