{"record":{"id":"81c33d6436b40465","repo":"OrchardCMS/OrchardCore","slug":"could-not-resolve-main-feature-for-type-dependency-name","errorCode":null,"errorMessage":"Could not resolve main feature for type {dependency.Name}.","messagePattern":"Could not resolve main feature for type (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore/Extensions/Features/TypeFeatureProvider.cs","lineNumber":29,"sourceCode":"    {\n        if (_features.TryGetValue(dependency, out var features))\n        {\n            return features.First().Extension;\n        }\n\n        throw new InvalidOperationException($\"Could not resolve extension for type {dependency.Name}.\");\n    }\n\n    public IFeatureInfo GetFeatureForDependency(Type dependency)\n    {\n        if (_features.TryGetValue(dependency, out var features))\n        {\n            // Gets the first feature that has the same ID as the extension, if any. \n            // Otherwise returns the first feature.\n            return features.FirstOrDefault(feature => feature.Extension.Id == feature.Id) ?? features.First();\n        }\n\n        throw new InvalidOperationException($\"Could not resolve main feature for type {dependency.Name}.\");\n    }\n\n    public IEnumerable<IFeatureInfo> GetFeaturesForDependency(Type dependency)\n    {\n        if (_features.TryGetValue(dependency, out var features))\n        {\n            return features;\n        }\n\n        throw new InvalidOperationException($\"Could not resolve features for type {dependency.Name}.\");\n    }\n\n    public IEnumerable<Type> GetTypesForFeature(IFeatureInfo feature)\n    {\n        return _features.Where(kv => kv.Value.Contains(feature)).Select(kv => kv.Key);\n    }\n\n    public void TryAdd(Type type, IFeatureInfo feature)","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore/Extensions/Features/TypeFeatureProvider.cs#L11-L47","documentation":"GetFeatureForDependency resolves the 'main' feature for a type: among the features mapped to the dependency type, it prefers the one whose Id equals its extension's Id (the module's root feature), falling back to the first feature. If the type is not in the map at all, this InvalidOperationException is thrown.","triggerScenarios":"Calling GetFeatureForDependency(type) where type was never added via TryAdd, so _features contains no entry for it.","commonSituations":"Querying a type registered outside module startup classes (e.g., host-level services); calling before shell composition completes; typo'd or renamed service type after a refactor.","solutions":["Confirm the type is contributed by a module Startup class so TryAdd runs for it.","Run the lookup only after shell/feature initialization is complete.","Guard the call by checking membership first (GetFeaturesForDependency / TryGetValue) and handling unknown types explicitly."],"exampleFix":"// before\nvar feature = provider.GetFeatureForDependency(typeof(UnknownService));\n\n// after\nvar known = provider.GetFeaturesForDependency(typeof(UnknownService)); // register UnknownService in a module Startup, or null-check first","handlingStrategy":"type-guard","validationCode":"// probe membership without throwing\nbool known;\ntry { provider.GetFeaturesForDependency(typeof(T)); known = true; } catch (InvalidOperationException) { known = false; }","typeGuard":"bool HasMainFeature(Type dependency) =>\n    provider.GetFeaturesForDependency(dependency)\n        ?.Any(f => f.Id == f.Extension.Id) == true;","tryCatchPattern":"try { var feature = provider.GetFeatureForDependency(type); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not resolve main feature\")) { feature = null; }","preventionTips":["Only query types registered via module Startup classes.","Ensure feature discovery has completed before querying.","Write a TryGet wrapper instead of scattering raw lookups."],"tags":["dependency-injection","modules","features"],"backgroundTag":"resource-not-found","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}