Skip to content

Dependency Injection Error #119

@justinmiller62

Description

@justinmiller62

Dependency Injection Error Prevents Application Startup on macOS/Linux

Description

The application fails to start on macOS (and likely Linux) due to a dependency injection lifetime mismatch. A singleton service (ExtendedConfig) is attempting to consume a scoped service (ISettingsProcessor), which violates ASP.NET Core's DI rules.

Environment

  • OS: macOS (Darwin 24.4.0) - ARM64
  • Docker: Desktop with ARM64 support
  • .NET Version: 9.0.8 (built from source)
  • Environment Mode: Development (also fails in Production)

Error Details

System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: AspNetCore.Authentication.Basic.IBasicUserValidationService Lifetime: Transient ImplementationType: AiCoreApi.Authorization.BasicUserValidationService': Cannot consume scoped service 'AiCoreApi.Data.Processors.ISettingsProcessor' from singleton 'AiCoreApi.Common.ExtendedConfig'.

Root Cause Analysis

The issue stems from incompatible service lifetimes in Startup.cs:

  1. Line 53-55: All interfaces matching ^I.*Processor$ are registered as Scoped

    services.ForInterfacesMatching("^I.*Processor$")
        .OfAssemblies(Assembly.GetExecutingAssembly())
        .AddScoped();
  2. Line 77: ExtendedConfig is registered as Singleton

    services.AddSingleton<ExtendedConfig>();
  3. ExtendedConfig.cs:22: The constructor depends on ISettingsProcessor

    public ExtendedConfig(ISettingsProcessor settingsProcessor)
    {
        _settingsProcessor = settingsProcessor;
    }

This violates ASP.NET Core's rule that singleton services cannot depend on scoped services.

Why This Works on Windows but Not macOS/Linux

The issue likely goes unnoticed on Windows because:

  • Development environments on Windows might default to Production mode where validation is less strict
  • The early call to services.BuildServiceProvider() on line 80 triggers validation before environment settings take effect
  • Windows deployments might have different configuration or modified code

Steps to Reproduce

  1. Clone the repository on macOS or Linux
  2. Build Docker images from source:
    docker build -f AiCoreApi/Dockerfile -t aicore-api:latest . --platform linux/arm64
  3. Run the container
  4. Observe the DI validation error preventing startup

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions