{"record":{"id":"0a3dc6fe30df3e44","repo":"abpframework/abp","slug":"an-object-accessor-is-registered-before-for-type","errorCode":null,"errorMessage":"An object accessor is registered before for type: {typeof(T).AssemblyQualifiedName}","messagePattern":"An object accessor is registered before for type: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionObjectAccessorExtensions.cs","lineNumber":33,"sourceCode":"\n        return services.AddObjectAccessor<T>();\n    }\n\n    public static ObjectAccessor<T> AddObjectAccessor<T>(this IServiceCollection services)\n    {\n        return services.AddObjectAccessor(new ObjectAccessor<T>());\n    }\n\n    public static ObjectAccessor<T> AddObjectAccessor<T>(this IServiceCollection services, T obj)\n    {\n        return services.AddObjectAccessor(new ObjectAccessor<T>(obj));\n    }\n\n    public static ObjectAccessor<T> AddObjectAccessor<T>(this IServiceCollection services, ObjectAccessor<T> accessor)\n    {\n        if (services.Any(s => s.ServiceType == typeof(ObjectAccessor<T>)))\n        {\n            throw new Exception(\"An object accessor is registered before for type: \" + typeof(T).AssemblyQualifiedName);\n        }\n\n        //Add to the beginning for fast retrieve\n        services.Insert(0, ServiceDescriptor.Singleton(typeof(ObjectAccessor<T>), accessor));\n        services.Insert(0, ServiceDescriptor.Singleton(typeof(IObjectAccessor<T>), accessor));\n\n        return accessor;\n    }\n\n    public static T? GetObjectOrNull<T>(this IServiceCollection services)\n        where T : class\n    {\n        return services.GetSingletonInstanceOrNull<IObjectAccessor<T>>()?.Value;\n    }\n\n    public static T GetObject<T>(this IServiceCollection services)\n        where T : class\n    {","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/abpframework/abp/blob/7ed43b1931b9df46a50c0c59148a18645641d0df/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionObjectAccessorExtensions.cs#L15-L51","documentation":"Thrown by ServiceCollectionObjectAccessorExtensions.AddObjectAccessor<T> when an ObjectAccessor<T> (or IObjectAccessor<T>) descriptor for the same T is already in the service collection. Object accessors are meant to hold a single shared instance per type during early startup (before the provider is built), so double-registration is treated as a programming error rather than silently overwritten.","triggerScenarios":"Calling services.AddObjectAccessor<T>() twice for the same T; a module and a host configuration both registering the same accessor type (e.g. IConfiguration, IAbpHostEnvironment).","commonSituations":"Custom startup code that registers IConfiguration or another accessor that ABP's framework already added; calling AddObjectAccessor in a module whose dependency also adds it; integrating a library that adds an accessor ABP also manages.","solutions":["Use TryAddObjectAccessor<T>() instead, which returns the existing accessor if one is already registered rather than throwing.","Remove the duplicate AddObjectAccessor call; let the earlier registration win.","Identify which component already registered the accessor (search the codebase for AddObjectAccessor<T>) and avoid re-adding it."],"exampleFix":"// before — throws if T already registered\nservices.AddObjectAccessor<IConfiguration>(config);\n\n// after — idempotent\nservices.TryAddObjectAccessor<IConfiguration>();\n// or ensure you only register once before this point","handlingStrategy":"validation","validationCode":"// Prefer the idempotent TryAddObjectAccessor to avoid duplicate-registration exceptions\nservices.TryAddObjectAccessor<T>();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use TryAddObjectAccessor<T>() whenever the accessor may already be registered.","Search the codebase for existing AddObjectAccessor<T> calls before adding your own.","Centralize accessor registration in one module to avoid scattered duplicates."],"tags":["dependency-injection","object-accessor","startup","abp-core"],"backgroundTag":null,"analyzedSha":"7ed43b1931b9df46a50c0c59148a18645641d0df","analyzedAt":"2026-08-13T16:26:11.351Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}