Skip to main content

Fixed: MAFGENWF003 Class 'CountWordsExecutor' contains [MessageHandler] methods but is not declared as partial

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

With the workflows source generator installed, one of my Microsoft Agent Framework executor classes failed to build with error MAFGENWF003: Class 'CountWordsExecutor' contains [MessageHandler] methods but is not declared as partial, and a CS0534 for the same class came with it. The class was missing one keyword: partial.

What I Was Doing​

I was building WordCount, a small console app with a two-step workflow: CleanText removes the extra spaces from a text, and CountWords counts its words. Both steps are class-based executors, classes that derive from Executor with one method marked [MessageHandler]. The project referenced Agent Framework's workflows package, Microsoft.Agents.AI.Workflows 1.28.0, and its source generator, Microsoft.Agents.AI.Workflows.Generators 1.28.0.

CleanTextExecutor was declared partial. CountWordsExecutor, on line 35 of Program.cs, was declared like an ordinary class:

Program.cs (the second executor, as it was)
// Step 2: takes a clean text and returns how many words it has.
internal sealed class CountWordsExecutor() : Executor("CountWords")
{
[MessageHandler]
private int Handle(string text, IWorkflowContext context) => text.Split(' ').Length;
}

What I Expected​

The app to build and print each executor's result, then the workflow's output: 4, the number of words. Nothing in the class's own code uses another part of a class, so I didn't expect it to need partial.

What I Got​

Terminal
dotnet run
Output
/Users/you/source/WordCount/Program.cs(35,23): error MAFGENWF003: Class 'CountWordsExecutor' contains [MessageHandler] methods but is not declared as partial
/Users/you/source/WordCount/Program.cs(35,23): error CS0534: 'CountWordsExecutor' does not implement inherited abstract member 'Executor.ConfigureProtocol(ProtocolBuilder)'

The build failed. Fix the build errors and run again.

macOS-style window with two panes. Above, the CountWordsExecutor class declared as internal sealed class, without partial, with one MessageHandler method that counts words. Below, dotnet run failing at Program.cs line 35 with error MAFGENWF003, Class CountWordsExecutor contains MessageHandler methods but is not declared as partial, and error CS0534 for Executor.ConfigureProtocol(ProtocolBuilder).

What I Checked​

Where the errors point. Both errors are at line 35, column 23: the name CountWordsExecutor in internal sealed class CountWordsExecutor(). CleanTextExecutor, declared partial in the same file, got no error.

Where MAFGENWF003 comes from. The ID and its message are in the workflows source generator, the analyzer that Microsoft.Agents.AI.Workflows.Generators adds to the build. A source generator is a tool that runs while the project compiles and adds code to it. This one found a [MessageHandler] method in a class it couldn't add code to, and said so.

What Microsoft's documentation requires. The Executors guide: The class must be marked partial to enable source generation. The remarks of MessageHandlerAttribute in the 1.28.0 package documentation say the same: The containing class must be partial and derive from Executor. My class derived from Executor but wasn't partial.

Why CS0534 came too. Executor has an abstract method, ConfigureProtocol(ProtocolBuilder), and the generator is what writes it, from the [MessageHandler] methods. It writes it as another part of the class, and C# joins the parts of a class only by this rule from Microsoft's C# guide: All the parts must use the partial keyword. So the generator left CountWordsExecutor alone, the method was never written, and the compiler reported the same CS0534 it gives when the generator package is missing (Fixed: CS0534 'CountWordsExecutor' does not implement inherited abstract member 'Executor.ConfigureProtocol(ProtocolBuilder)').

The Fix​

Declare the class partial:

Program.cs (the second executor)
// Step 2: takes a clean text and returns how many words it has.
internal sealed partial class CountWordsExecutor() : Executor("CountWords")
{
[MessageHandler]
private int Handle(string text, IWorkflowContext context) => text.Split(' ').Length;
}

The keyword goes right before class, after internal and sealed. C# is strict about that: The partial modifier can only appear immediately before the keyword class, struct, or interface.

After the Fix​

Terminal
dotnet run
Output
CleanText returned: the quick brown fox
CountWords returned: 4
The workflow's output: 4

macOS-style window with two panes. Above, the CountWordsExecutor class declared as internal sealed partial class with its MessageHandler method. Below, dotnet run printing CleanText returned: the quick brown fox, CountWords returned: 4, and The workflow's output: 4.

Both errors are gone. With partial, the generator added its part to CountWordsExecutor, as it already did for CleanTextExecutor, and the workflow counted four words.

Common Mistakes​

MistakeWhat happensDo this instead
Declaring an executor class with [MessageHandler] methods but no partialMAFGENWF003, plus CS0534 for that classDeclare it partial
Putting partial before the other modifiers, as in partial internal sealed classC# doesn't allow it: partial can only appear immediately before classWrite internal sealed partial class
Adding partial without the generator packageCS0534 on every executor class (Fixed)Reference Microsoft.Agents.AI.Workflows.Generators too

Troubleshooting​

SymptomLikely causeFix
MAFGENWF003 and CS0534 at the same line and columnThat class has [MessageHandler] methods and isn't partialAdd partial before class
CS0534 on every executor class, with no MAFGENWF003The generator package isn't in the projectAdd Microsoft.Agents.AI.Workflows.Generators

FAQ​

Can an executor class stay internal and sealed?​

Yes. partial only adds to the declaration. Microsoft's own examples declare executors as internal sealed partial class, the same as CleanTextExecutor and CountWordsExecutor here.

Do other attributes need partial too?​

Yes. Microsoft's Executors guide says that with the generator referenced, a class with the class-level [SendsMessage] or [YieldsOutput] attributes must be partial as well, so the generator can add its protocol configuration.

Conclusion​

MAFGENWF003: Class 'CountWordsExecutor' contains [MessageHandler] methods but is not declared as partial means the workflows source generator found handler methods in a class it can't add code to. The CS0534 next to it follows from that: the method the generator would have written is missing. Add partial right before class, and both errors go away.

Additional Resources​