abpframework/abp · critical · AbpException

Service provider was already set before to another service p

Error message

Service provider was already set before to another service provider instance.

What it means

AbpApplicationWithExternalServiceProvider is used when ABP is hosted inside an external DI container (e.g. ASP.NET Core's provider). SetServiceProvider may bind the provider only once; calling it again with a DIFFERENT instance throws AbpException. The ServiceProvider field is declared 'default!' (null with a null-forgiving operator), so despite the nullable annotation it is null at runtime until first set - the guard checks that runtime nullness. Calling SetServiceProvider again with the SAME instance is a tolerated no-op.

Source

Thrown at framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs:31

        Action<AbpApplicationCreationOptions>? optionsAction
        ) : base(
            startupModuleType,
            services,
            optionsAction)
    {
        services.AddSingleton<IAbpApplicationWithExternalServiceProvider>(this);
    }

    void IAbpApplicationWithExternalServiceProvider.SetServiceProvider([NotNull] IServiceProvider serviceProvider)
    {
        Check.NotNull(serviceProvider, nameof(serviceProvider));

        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
        if (ServiceProvider != null)
        {
            if (ServiceProvider != serviceProvider)
            {
                throw new AbpException("Service provider was already set before to another service provider instance.");
            }

            return;
        }

        SetServiceProvider(serviceProvider);
    }

    public async Task InitializeAsync(IServiceProvider serviceProvider)
    {
        Check.NotNull(serviceProvider, nameof(serviceProvider));

        SetServiceProvider(serviceProvider);

        await InitializeModulesAsync();
        
        await SetupTelemetryTrackingAsync();
    }

View on GitHub (pinned to 7ed43b1931)

Solutions

  1. Ensure SetServiceProvider (or Initialize/InitializeAsync, which call it) is invoked exactly once, with the same provider.
  2. If a reset is genuinely needed, dispose the app and create a new AbpApplication instance rather than reassigning the provider.
  3. Audit Program.cs / the test fixture for two init calls and remove the duplicate.

Example fix

// before
((IAbpApplicationWithExternalServiceProvider)app).SetServiceProvider(providerA);
app.Initialize(providerB); // throws

// after
var app = AbpApplicationFactory.Create<MyModule>(services);
app.Initialize(providerA); // single call with one provider
Defensive patterns

Strategy: validation

Validate before calling

// Bind the provider exactly once; the same instance is tolerated as a no-op:
var app = AbpApplicationFactory.Create<MyModule>(services);
var provider = services.BuildServiceProvider();
((IAbpApplicationWithExternalServiceProvider)app).SetServiceProvider(provider);
// do NOT call Initialize again with a different provider

Prevention

When it happens

Trigger: Calling app.SetServiceProvider(providerA) then app.SetServiceProvider(providerB) where B != A; or calling both InitializeAsync(provider) and an explicit SetServiceProvider(differentProvider).

Common situations: Double-init in Program.cs; a test fixture reusing an app across two providers; calling both the IAbpApplicationWithExternalServiceProvider.SetServiceProvider and Initialize paths with different providers; middleware that re-invokes init.

Related errors


AI-assisted analysis of abpframework/abp@7ed43b1931 (2026-08-13). Data as JSON: /api/errors/d8ee49828be903e0. Report an issue: GitHub.