dotnet/wpf · error · CspProjectException

Error: Type ' ' has no Main method

Error message

Error: Type '{0}' has no Main method

What it means

Project.ExecuteMain in the csp tool throws CspProjectException when the resolved main class type does not contain a Main method (IsMainPresent returns false). The csp tool invokes the class's Main as the project's entry point, so a class without a valid Main makes execution impossible.

Solutions

  1. Add a valid entry point to the class: `static void Main(string[] args)` (or the exact signature csp expects).
  2. Point the project's mainClass setting at a class that already has a Main method.
  3. Restore the original Main signature if it was changed to an unsupported form.

Example fix

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

Strategy: validation

Validate before calling

var mainType = asm.GetType(sMainClass);
var hasMain = mainType?.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
    .Any(m => m.Name == "Main") ?? false;
if (!hasMain) throw new Exception($"{sMainClass} has no Main method");

Type guard

static bool HasMain(Type t) => t?.GetMethod("Main", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) != null;

Try / catch

try { project.ExecuteMain(); }
catch (CspProjectException ex) when (ex.Message.Contains("has no Main method")) { /* add a static Main to the class or point the project at another class */ }

Prevention

When it happens

Trigger: The type declared as the project's main class exists in the assembly but defines no static Main method (with a signature csp recognizes), e.g. entry point renamed, made non-static, or given an unsupported parameter list.

Common situations: Refactoring Main into another method name; converting Main to async Main or adding parameters the tool does not accept; pointing the project at a class that is a library type rather than an entry-point class.

Related errors


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

Appendix: source

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

            )
        {
            if (sMainClass == null)
            {
                sMainClass = FindMainClass();
            }

            Object returnValue = null;

            Type tMainClass = _assembly.GetType(sMainClass);
            if (tMainClass == null)
            {
                throw new CspProjectException(
                    "Error: Project does not contain type '" + sMainClass +"'");
            }

            if (!IsMainPresent(tMainClass))
            {
                throw new CspProjectException(
                    "Error: Type '" + sMainClass +"' has no Main method");
            }
            
            try
            {
                if (_breakBeforeInvoke)
                {
                    // This breakpoint is just before we execute the project we've compiled.
                    // If you want to debug the project, step into the following "InvokeMember"
                    // call.

                    System.Diagnostics.Debugger.Break();
                }

                returnValue = 
                tMainClass.InvokeMember(
                    "Main",
                    BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,

View on GitHub (pinned to 81131a70a4)