abpframework/abp · error · AbpException
ServiceConfigurationContext is only available in the Configu
Error message
ServiceConfigurationContext is only available in the ConfigureServices, PreConfigureServices and PostConfigureServices methods.
What it means
Thrown by the AbpModule.ServiceConfigurationContext getter when the backing _serviceConfigurationContext is null — i.e. the property is accessed outside the DI-configuration phase. ABP sets the context only during PreConfigureServices/ConfigureServices/PostConfigureServices, so reading it elsewhere is illegal. It is an AbpException.
Source
Thrown at framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModule.cs:24
namespace Volo.Abp.Modularity;
public abstract class AbpModule :
IAbpModule,
IOnPreApplicationInitialization,
IOnApplicationInitialization,
IOnPostApplicationInitialization,
IOnApplicationShutdown,
IPreConfigureServices,
IPostConfigureServices
{
protected internal bool SkipAutoServiceRegistration { get; protected set; }
protected internal ServiceConfigurationContext ServiceConfigurationContext {
get {
if (_serviceConfigurationContext == null)
{
throw new AbpException($"{nameof(ServiceConfigurationContext)} is only available in the {nameof(ConfigureServices)}, {nameof(PreConfigureServices)} and {nameof(PostConfigureServices)} methods.");
}
return _serviceConfigurationContext;
}
internal set => _serviceConfigurationContext = value;
}
private ServiceConfigurationContext? _serviceConfigurationContext;
public virtual Task PreConfigureServicesAsync(ServiceConfigurationContext context)
{
PreConfigureServices(context);
return Task.CompletedTask;
}
public virtual void PreConfigureServices(ServiceConfigurationContext context)
{
View on GitHub (pinned to 7ed43b1931)
Solutions
- Move the offending call into PreConfigureServices, ConfigureServices, or PostConfigureServices of the module.
- For runtime configuration use IOptions<T> or IConfigureOptions<T> instead of touching ServiceConfigurationContext.Services.
- If you need a value computed at startup, capture it in ConfigureServices and store it for later use.
- Check that the method you put the code in actually receives/uses the ServiceConfigurationContext parameter.
Example fix
// before
public class MyModule : AbpModule
{
public MyModule() => Services.AddSingleton<X>(); // wrong: in constructor
}
// after
public class MyModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddSingleton<X>();
}
} Defensive patterns
Strategy: validation
Validate before calling
// Do not access ServiceConfigurationContext outside config phase.
// Guard by only reading it inside ConfigureServices:
public override void ConfigureServices(ServiceConfigurationContext context)
{
var services = context.Services; // safe here
} Try / catch
try { Configure<MyOptions>(o => { }); }
catch (AbpException ex) when (ex.Message.Contains("only available"))
{
logger.LogError(ex, "Configure called outside the services-config phase");
} Prevention
- Only read ServiceConfigurationContext inside Pre/Configure/PostConfigureServices.
- For runtime needs, use IOptions<T> / IConfigureOptions<T>, not the context.
- Never call Configure<TOptions> helpers from a module constructor.
When it happens
Trigger: Accessing ServiceConfigurationContext (or the Configure<TOptions> helpers that route through it) from a module constructor, a field initializer, an IOnApplicationInitialization handler, or any code that runs before/after the config phase.
Common situations: Calling Services.Configure(...) or reading context.Services inside a module constructor or a static helper invoked at load time; refactoring that moved a Configure call out of ConfigureServices.
Related errors
- An error occurred during the initialize {contributor.GetType
- An error occurred during the shutdown {contributor.GetType()
- Could not find singleton service: {typeof(T).AssemblyQualifi
- serviceKey can not be null! Use ExposeServicesAttribute inst
- Given type is not an ABP module: {moduleType.AssemblyQualifi
AI-assisted analysis of abpframework/abp@7ed43b1931 (2026-08-13).
Data as JSON: /api/errors/6658bbc55e754ceb.
Report an issue: GitHub.