Search code examples
c#entity-frameworkdependency-injection.net-coreconstructor-injection

Building a Service Layer in .NET Core WebApp. Constructor DI isnt working


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).

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.

To do this i register first my Repositories like this:

Startup.cs

public void ConfigureServices(IServiceCollection services) {
    this.storageConnectionString = "Server=.;Initial Catalog=MyDatabase;Integrated Security=True;";
    services.AddScoped<IDeviceRepository>(r => new EntityDeviceRepository(this.storageConnectionString));
    services.AddScoped<IMasterDataRepository>(r => new EntityMasterDataRepository(this.storageConnectionString));
    ...

(in the same file and function) i register my Services like this:

    var serviceProvider = services.BuildServiceProvider();
    services.AddScoped<IDeviceService>(r => new DeviceService(
        serviceProvider.GetService<IDeviceRepository>(),
        serviceProvider.GetService<IMasterDataRepository>(),
        ));

    services.AddScoped<IHardwareDataService>(r => new HardwareDataService(
        serviceProvider.GetService<IDeviceService>(),
        serviceProvider.GetService<IHardwareDataRepository>()
        ));
    ...
}

IHardwareDataService.cs

public interface IHardwareDataService
{
    Task<Boolean> UpdateHardwareData(HardwareData hardwareData);
}

HardwareDataService.cs

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<Boolean> 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)
    }
}

The Problem

The "IDeviceService" is working without Problems. The "IHardwareDataService" not. To be exact the Object "IDeviceService" is null.

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.

Any tip about how I can solve this Problem?


Solution

  • There is really no need to build the provider in this scenario.

    Review how you register the services, which can be simplified to...

    services.AddScoped<IDeviceService, DeviceService>();
    services.AddScoped<IHardwareDataService, HardwareDataService>();
    

    The framework will inject the necessary dependencies when resolving the implementations.

    Reference: Introduction to Dependency Injection in ASP.NET Core: Registering Your Own Services