title: Dependency injection net core console application setup
body: <p>I am trying to use dependency injection for a .Net Core Console application using the built in DI.</p>
  
  <p>I have 2 following Methods :</p>
  
  <pre><code>    private static void RegisterServices()
      {
          var collection = new ServiceCollection();
  
          //repositories
          collection.AddScoped&lt;IAccountDataRepository, AccountDataRepository&gt;();
          collection.AddScoped&lt;IClientDataRepository, ClientDataRepository&gt;();
          collection.AddScoped&lt;IAddressDataRepository, AddressDataRepository&gt;();
          collection.AddScoped&lt;IClientAccountDataRepository, ClientAccountDataRepository&gt;();
          //services
          collection.AddScoped&lt;IAccountDataService, AccountDataService&gt;();
          collection.AddScoped&lt;IClientDataService, ClientDataService&gt;();
          collection.AddScoped&lt;IAddressDataService, AddressDataService&gt;();
          collection.AddScoped&lt;IClientAccountDataService, ClientAccountDataService&gt;();
  
          _serviceProvider = collection.BuildServiceProvider();
  
      }
  
      private static void DisposeServices()
      {
          if (_serviceProvider == null)
          {
              return;
          }
          if (_serviceProvider is IDisposable)
          {
              ((IDisposable)_serviceProvider).Dispose();
          }
      }
  </code></pre>
  
  <p>I can get this to work in the main method by using this:</p>
  
  <pre><code>private static IServiceProvider _serviceProvider;
  private static IClientDataRepository _clientDataRepository;
  
  
  
  static void Main(string[] args)
  {
  
      RegisterServices();
  
      _clientDataRepository = _serviceProvider.GetService&lt;IClientDataRepository&gt;();
  </code></pre>
  
  <p>However I need to inject the repository to the service and the service to main but I can t use the following in the service class :</p>
  
  <pre><code>_clientDataRepository = _serviceProvider.GetService&lt;IClientDataRepository&gt;();
  </code></pre>
  
  <p>Here is service:</p>
  
  <pre><code>public class ClientDataService : IClientDataService
  {
      private readonly ILogger _logger;
      private readonly IClientDataRepository _clientDataRepository;
  
      public ClientDataService(ILogger logger, IClientDataRepository clientDataRepository)
      {
          _logger = logger;
          _clientDataRepository = clientDataRepository;
      }
  </code></pre>
  
  <p>If I use </p>
  
  <pre><code>_clientDataRepository = _serviceProvider.GetService&lt;IClientDataRepository&gt;();
  </code></pre>
  
  <p>will give an error</p>