dotnet/wpf · error · CspProjectException

Error: No Main method found

Error message

Error: No Main method found

What it means

After scanning every type in the assembly via reflection, FindMainClass (Project.cs:269) found no class with a public static Main method. Since the csp tool's ExecuteMain flow needs an entry point to run, it throws CspProjectException. Note the lookup is restricted to public static Main; other signatures are ignored.

Solutions

  1. Add a public static void Main(string[] args) method to the class that should be the entry point.
  2. Make the existing Main public and static (internal or private Main is not found by this reflection lookup).
  3. Verify you are passing the correct assembly — an executable, not a class library.
  4. Check the Main signature: the reflection call uses only the name 'Main' with Public|Static flags, so overloaded/renamed entry points must be adjusted.

Example fix

// before
internal class Program { static void Main(string[] args) { ... } }

// after
public class Program { public static void Main(string[] args) { ... } }
Defensive patterns

Strategy: validation

Validate before calling

bool hasMain = assembly.GetTypes().Any(t =>
    t.GetMethod("Main", BindingFlags.Public | BindingFlags.Static) != null);
if (!hasMain)
    throw new InvalidOperationException("Assembly has no public static Main; not an executable.");

Try / catch

try { project.ExecuteMain(); }
catch (CspProjectException ex) when (ex.Message == "Error: No Main method found") {
    Console.Error.WriteLine("Target assembly has no public static Main; pass an executable assembly.");
}

Prevention

When it happens

Trigger: ExecuteMain is called on an assembly that contains no type with a public static Main method, i.e. FindMainClass iterated all types without ever setting sRet.

Common situations: Pointing the tool at a class library (DLL) instead of an executable, an entry point declared as private/internal static Main, an async Task Main not matching the expected shape, or top-level-statements/renamed entry methods that never produce a public static Main.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6df5d372fefe0be7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/tools/csp/Project.cs:290

            foreach (Type t in _assembly.GetTypes())
            {
                if (IsMainPresent(t))
                {
                    if (sRet != "")
                    {
                        throw new CspProjectException(
                            "Error: Multiple Main methods - in classes '" + sRet + "' and '" + t.FullName + "'");
                    }

                    sRet = t.FullName;
                }

            }

            if (sRet == "")
            {
                throw new CspProjectException(
                    "Error: No Main method found");
            }

            return sRet;
        }


        /// <summary>
        /// Tests whether the type has a Main method.
        /// Throws an exception if more than one is present.
        /// </summary>
        private bool IsMainPresent(Type t)
        {
            MethodInfo method = null;
            try
            {
                method = t.GetMethod(
                    "Main",

View on GitHub (pinned to 81131a70a4)