{"record":{"id":"6658bbc55e754ceb","repo":"abpframework/abp","slug":"serviceconfigurationcontext-is-only-available-in-t","errorCode":null,"errorMessage":"ServiceConfigurationContext is only available in the ConfigureServices, PreConfigureServices and PostConfigureServices methods.","messagePattern":"ServiceConfigurationContext is only available in the ConfigureServices, PreConfigureServices and PostConfigureServices methods\\.","errorType":"exception","errorClass":"AbpException","httpStatus":null,"severity":"error","filePath":"framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModule.cs","lineNumber":24,"sourceCode":"\nnamespace Volo.Abp.Modularity;\n\npublic abstract class AbpModule :\n    IAbpModule,\n    IOnPreApplicationInitialization,\n    IOnApplicationInitialization,\n    IOnPostApplicationInitialization,\n    IOnApplicationShutdown,\n    IPreConfigureServices,\n    IPostConfigureServices\n{\n    protected internal bool SkipAutoServiceRegistration { get; protected set; }\n\n    protected internal ServiceConfigurationContext ServiceConfigurationContext {\n        get {\n            if (_serviceConfigurationContext == null)\n            {\n                throw new AbpException($\"{nameof(ServiceConfigurationContext)} is only available in the {nameof(ConfigureServices)}, {nameof(PreConfigureServices)} and {nameof(PostConfigureServices)} methods.\");\n            }\n\n            return _serviceConfigurationContext;\n        }\n        internal set => _serviceConfigurationContext = value;\n    }\n\n    private ServiceConfigurationContext? _serviceConfigurationContext;\n\n    public virtual Task PreConfigureServicesAsync(ServiceConfigurationContext context)\n    {\n        PreConfigureServices(context);\n        return Task.CompletedTask;\n    }\n\n    public virtual void PreConfigureServices(ServiceConfigurationContext context)\n    {\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/abpframework/abp/blob/7ed43b1931b9df46a50c0c59148a18645641d0df/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModule.cs#L6-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic class MyModule : AbpModule\n{\n    public MyModule() => Services.AddSingleton<X>(); // wrong: in constructor\n}\n\n// after\npublic class MyModule : AbpModule\n{\n    public override void ConfigureServices(ServiceConfigurationContext context)\n    {\n        context.Services.AddSingleton<X>();\n    }\n}","handlingStrategy":"validation","validationCode":"// Do not access ServiceConfigurationContext outside config phase.\n// Guard by only reading it inside ConfigureServices:\npublic override void ConfigureServices(ServiceConfigurationContext context)\n{\n    var services = context.Services; // safe here\n}","typeGuard":null,"tryCatchPattern":"try { Configure<MyOptions>(o => { }); }\ncatch (AbpException ex) when (ex.Message.Contains(\"only available\"))\n{\n    logger.LogError(ex, \"Configure called outside the services-config phase\");\n}","preventionTips":["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."],"tags":["modularity","di","lifecycle","configure","abp-core"],"backgroundTag":null,"analyzedSha":"7ed43b1931b9df46a50c0c59148a18645641d0df","analyzedAt":"2026-08-13T16:26:11.351Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}