abpframework/abp · error · Exception
"public class" declaration not found!
Error message
"public class" declaration not found!
What it means
ModuleClassDependcyAdder.GetIndexOfWhereDependsOnWillBeAdded searches the module file text for the literal 'public class' and throws if IndexOf returns -1. Adding a [DependsOn] attribute requires inserting it immediately before the module class declaration; if the class is declared differently (static, internal, partial-first-line, file-scoped namespace reformatted, or file is empty/corrupt), the insertion point cannot be located.
Source
Thrown at framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs:47
File.WriteAllText(path, file);
}
protected virtual string InsertDependsOnAttribute(string file, string moduleName)
{
var indexOfPublicClassDeclaration = GetIndexOfWhereDependsOnWillBeAdded(file);
var dependsOnAttribute = GetDependsOnAttribute(moduleName);
return file.Insert(indexOfPublicClassDeclaration, dependsOnAttribute);
}
protected virtual int GetIndexOfWhereDependsOnWillBeAdded(string file)
{
var indexOfPublicClassDeclaration = file.IndexOf("public class", StringComparison.Ordinal);
if (indexOfPublicClassDeclaration < 0)
{
throw new Exception("\"public class\" declaration not found!");
}
return indexOfPublicClassDeclaration;
}
protected virtual string GetDependsOnAttribute(string moduleName)
{
return "[DependsOn(typeof(" + moduleName + "))]" + Environment.NewLine + " ";
}
protected virtual void ParseModuleNameAndNameSpace(string module, out string nameSpace, out string moduleName)
{
var words = module?.Split('.');
if (words == null || words.Length <= 1)
{
nameSpace = null;
moduleName = module;View on GitHub (pinned to 7ed43b1931)
Solutions
- Open the target module .cs file and ensure it contains a declaration literally starting with 'public class' (e.g. change 'internal class MyModule : AbpModule' to 'public class MyModule : AbpModule').
- Avoid 'public partial class' patterns that break the substring match, or add the [DependsOn] attribute manually above the class declaration.
- Re-run 'abp add-package <pkg>' once the declaration matches the expected pattern.
Example fix
// before
internal class MyProjectModule : AbpModule { }
// after
[DependsOn(typeof(AbpSomePackageModule))]
public class MyProjectModule : AbpModule { } Defensive patterns
Strategy: validation
Validate before calling
var moduleSrc = File.ReadAllText(moduleFile);
if (!moduleSrc.Contains("public class", StringComparison.Ordinal))
{
Console.Error.WriteLine($"{moduleFile} has no 'public class' declaration; add the [DependsOn] manually.");
return;
} Type guard
static bool HasPublicClassDeclaration(string source) =>
source.Contains("public class", StringComparison.Ordinal); Prevention
- Keep module classes declared as 'public class' without intervening keywords like 'partial'.
- Add [DependsOn] attributes manually in code review rather than relying on the auto-wirer for non-standard declarations.
When it happens
Trigger: Calling ModuleClassDependcyAdder.Add (triggered by 'abp add-package' / ProjectNugetPackageAdder) on a file whose module class is declared 'internal class', 'public sealed partial class', or where the class declaration was altered so the literal substring 'public class' is absent.
Common situations: A module class declared 'public partial class' where 'public class' is split across the partial keyword; an 'internal' module class; a manually edited or auto-formatted file that changed visibility; a generated file that omits the public modifier.
Related errors
- Could not find a class derived from AbpModule in the project
- Should specify an option name after '--' prefix!
- Should specify an option name after '-' prefix!
- Option names should start with '-' or '--'.
- There are multiple classes derived from AbpModule in the pro
AI-assisted analysis of abpframework/abp@7ed43b1931 (2026-08-13).
Data as JSON: /api/errors/cadf7b7234ced3ed.
Report an issue: GitHub.