Fixed: The workflow factory returned workflow with name '', but the expected name is 'greeting'
I registered a Microsoft Agent Framework workflow in ASP.NET Core with AddWorkflow, and the first request that used it answered 500 with System.InvalidOperationException: The workflow factory returned workflow with name '', but the expected name is 'greeting'. The function that builds the workflow has to name it, with exactly the name AddWorkflow passes to that function.
What I Was Doing
I was building a small ASP.NET Core API, GreetingApi, that registers a workflow with AddWorkflow and runs it from an endpoint. AddWorkflow comes from Microsoft.Agents.AI.Hosting, a preview package (1.28.0-preview.261106.1). The workflow has one step, a function that greets a person, and my first registration built it like this:
// Register the workflow under its name. The function builds it once, the first time it's needed.
builder.AddWorkflow("greeting", (services, name) =>
{
Func<string, string> greet = person => $"Hello, {person}!";
// threadsafe: the function keeps nothing between messages, so several requests can share it.
ExecutorBinding greetPerson = greet.BindAsExecutor("GreetPerson", threadsafe: true);
return new WorkflowBuilder(greetPerson)
.WithOutputFrom(greetPerson)
.Build();
});
The endpoint asks for the workflow by that name and runs it:
app.MapGet("/greet/{person}", async (string person, [FromKeyedServices("greeting")] Workflow workflow) =>
{
await using Run run = await InProcessExecution.Concurrent.RunAsync(workflow, person);
string? greeting = run.OutgoingEvents.OfType<WorkflowOutputEvent>().Select(output => output.Data as string).FirstOrDefault();
return greeting is null ? Results.Problem("The workflow returned nothing.") : Results.Ok(greeting);
});
The whole app, step by step, is in How to Register a Microsoft Agent Framework Workflow in ASP.NET Core with AddWorkflow.
What I Expected
200 OK and the greeting "Hello, Ana!" for GET /greet/Ana.
What I Got
The API started, but the first request failed:
HTTP/1.1 500 - Internal Server Error
content-type: text/plain; charset=utf-8
server: Kestrel
transfer-encoding: chunked
System.InvalidOperationException: The workflow factory returned workflow with name '', but the expected name is 'greeting'.
at Microsoft.Agents.AI.Hosting.HostApplicationBuilderWorkflowExtensions.<>c__DisplayClass0_0.<AddWorkflow>b__0(IServiceProvider sp, Object key)
at Microsoft.Agents.AI.Hosting.AgentHostingServiceCollectionExtensions.<>c__DisplayClass5_0`1.<AddKeyedService>b__0(IServiceProvider sp, Object key)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetRequiredKeyedService(Type serviceType, Object serviceKey, ServiceProviderEngineScope serviceProviderEngineScope)
at lambda_method1(Closure, Object, HttpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
The rest of the body lists the request's headers.

What I Checked
Where it was thrown. The first frame is a function inside AddWorkflow, in HostApplicationBuilderWorkflowExtensions. AddKeyedService called it while dependency injection ran GetRequiredKeyedService for the endpoint's request delegate, lambda_method1(Closure, Object, HttpContext). So the workflow was being created for my request, not when the app started.
What the documentation of AddWorkflow says. In Microsoft.Agents.AI.Hosting 1.28.0-preview.261106.1, AddWorkflow lists this exception: InvalidOperationException, Thrown when the factory delegate returns null or a workflow with a name that doesn't match the expected name. About the function itself, it says The function receives the service provider and workflow name as parameters. The name it passes is the one I registered, greeting. My workflow had no name at all, which the message shows as ''.
A name that doesn't match. To see that the check compares names, I gave the workflow a name of my own in the same place:
return new WorkflowBuilder(greetPerson)
.WithName("hello")
.WithOutputFrom(greetPerson)
.Build();
The same request failed the same way, with the name I'd typed:
HTTP/1.1 500 - Internal Server Error
content-type: text/plain; charset=utf-8
server: Kestrel
transfer-encoding: chunked
System.InvalidOperationException: The workflow factory returned workflow with name 'hello', but the expected name is 'greeting'.
The API's console shows that it started normally and failed only when the request arrived:
Using launch settings from /Users/you/source/GreetingApi/Properties/launchSettings.json...
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5010
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/you/source/GreetingApi
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The workflow factory returned workflow with name 'hello', but the expected name is 'greeting'.
at Microsoft.Agents.AI.Hosting.HostApplicationBuilderWorkflowExtensions.<>c__DisplayClass0_0.<AddWorkflow>b__0(IServiceProvider sp, Object key)
The same stack trace follows.
Why It Happens
AddWorkflow registers the workflow in dependency injection as a keyed service, with its name as the key: that's the AddKeyedService frame in the trace. It doesn't build the workflow then. The workflow's lifetime defaults to Singleton, and a singleton is created the first time something asks for it, which here was the endpoint. At that moment AddWorkflow calls your function, compares the name of the workflow it returns with the name it registered, and throws when they differ.
A WorkflowBuilder gives a workflow no name of its own. Without WithName, the name is empty, and the check fails with ''.
The Fix
Name the workflow with the name that AddWorkflow passes to the function:
// Register the workflow under its name. The function builds it once, the first time it's needed.
builder.AddWorkflow("greeting", (services, name) =>
{
Func<string, string> greet = person => $"Hello, {person}!";
// threadsafe: the function keeps nothing between messages, so several requests can share it.
ExecutorBinding greetPerson = greet.BindAsExecutor("GreetPerson", threadsafe: true);
return new WorkflowBuilder(greetPerson)
.WithName(name)
.WithOutputFrom(greetPerson)
.Build();
});
WithName(name) rather than WithName("greeting") means the name is typed once, in the AddWorkflow call, so the two can't drift apart.
After the Fix
The API started as before, and the same request returned the greeting:
HTTP/1.1 200 - OK
content-type: application/json; charset=utf-8
server: Kestrel
transfer-encoding: chunked
"Hello, Ana!"

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Building the workflow without WithName | The first request that needs it fails with 500, and the message shows the name '' | Call .WithName(name) |
Typing a name into WithName | Any name other than the registered one fails the same way | Pass the function's name parameter |
| Checking only that the app starts | It starts fine, because the workflow is built at the first request that needs it | Send a request after every change to the registration |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The workflow factory returned workflow with name '', but the expected name is 'greeting'. | The workflow has no name | Add .WithName(name) |
The same message with another name, such as 'hello' | WithName got a different name from the one in AddWorkflow | Pass the name parameter |
| The API starts without an error, and the error comes with the first request | The workflow is created when it's first needed | Look at the response or the API's console after a request |
FAQ
Why didn't the app fail when it started?
Because nothing had asked for the workflow yet. AddWorkflow registers it with a lifetime that defaults to Singleton, and Microsoft's documentation on service lifetimes lists two moments when a singleton is created: the first time it's requested, or when you give the container an instance yourself. AddWorkflow takes a function, not an instance, so the first request is that moment.
Does the endpoint's key have to match too?
Yes. [FromKeyedServices("greeting")] asks for the service registered under greeting; the attribute Indicates that the parameter should be bound using the keyed service registered with the specified key. So the one name appears in three places: in AddWorkflow, in WithName through the name parameter, and in the endpoint's attribute.
Conclusion
The workflow factory returned workflow with name '', but the expected name is 'greeting'. means the function you gave AddWorkflow built a workflow without that name. AddWorkflow checks the name when it first creates the workflow, at the first request that needs it, so the app starts fine and the request fails with 500. Build the workflow with .WithName(name), using the name AddWorkflow passes in, and the request returns its result.
Additional Resources
- Self-host Agent Framework applications — what
AddWorkflowand the hosting package do. - Service lifetimes — when a singleton is created.
