title: Building a Service Layer in .NET Core WebApp. Constructor DI isnt working
body: <p>i'd like to implement a Service Layer within my .NET Core WebApp but i have some problems with the Constructor Dependency Injection and no clue what i am doing wrong. Maybe someone can show me my mistake(s).</p>
  
  <p>The Idea about the Services is that they hold the Business Logic and are the only ones which speak to Repositories. Due the Fact every Service has its own responsibility, they also have to speak not only with their Repositories but also with other Services depending on which Business Task must be achived. </p>
  
  <p>To do this i register first my Repositories like this:</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services) {
      this.storageConnectionString = "Server=.;Initial Catalog=MyDatabase;Integrated Security=True;";
      services.AddScoped&lt;IDeviceRepository&gt;(r =&gt; new EntityDeviceRepository(this.storageConnectionString));
      services.AddScoped&lt;IMasterDataRepository&gt;(r =&gt; new EntityMasterDataRepository(this.storageConnectionString));
      ...
  </code></pre>
  
  <p>(in the same file and function) i register my Services like this:</p>
  
  <pre><code>    var serviceProvider = services.BuildServiceProvider();
      services.AddScoped&lt;IDeviceService&gt;(r =&gt; new DeviceService(
          serviceProvider.GetService&lt;IDeviceRepository&gt;(),
          serviceProvider.GetService&lt;IMasterDataRepository&gt;(),
          ));
  
      services.AddScoped&lt;IHardwareDataService&gt;(r =&gt; new HardwareDataService(
          serviceProvider.GetService&lt;IDeviceService&gt;(),
          serviceProvider.GetService&lt;IHardwareDataRepository&gt;()
          ));
      ...
  }
  </code></pre>
  
  <p><strong>IHardwareDataService.cs</strong></p>
  
  <pre><code>public interface IHardwareDataService
  {
      Task&lt;Boolean&gt; UpdateHardwareData(HardwareData hardwareData);
  }
  </code></pre>
  
  <p><strong>HardwareDataService.cs</strong></p>
  
  <pre><code>public class HardwareDataService : BaseService, IHardwareDataService
  {
      private IDeviceService deviceService;
      private IHardwareDataRepository hardwareDataRepository;
  
      public HardwareDataService(IDeviceService deviceService, IHardwareDataRepository hardwareDataRepository)
      {
          this.deviceService = deviceService;
          this.hardwareDataRepository = hardwareDataRepository;
      }
  
      public async Task&lt;Boolean&gt; UpdateHardwareData(HardwareData hardwareData)
      {
          // Create the Device via the Device Service if not already existing (Business Logic)
          // Update Hardware Data of Device (Business Logic + direct Access via HWDataRepository)
      }
  }
  </code></pre>
  
  <p><strong>The Problem</strong></p>
  
  <p>The "<code>IDeviceService</code>" is working without Problems.
  The "<code>IHardwareDataService</code>" not. To be exact the Object "<code>IDeviceService</code>" is <code>null</code>. </p>
  
  <p>It seems like i can't provide a Service to another Service via Constructor Dependency Injection, but i don't understand why and how to make it the right way. </p>
  
  <p>Any tip about how I can solve this Problem?</p>