Fixed: "error CS0104: 'IMiddleware' is an ambiguous reference between 'Microsoft.Agents.Builder.IMiddleware' and 'Microsoft.AspNetCore.Http.IMiddleware'"
I wrote my first middleware for a Microsoft 365 Agents SDK agent, a class that implements the SDK's IMiddleware interface, and the build stopped with error CS0104. ASP.NET Core has an IMiddleware interface too, and the web project had imported it into every file without my asking. A one-line alias fixed it.
What I Was Doing
I was building the bill-splitting agent for Part 3 of the Microsoft 365 Agents SDK Fundamentals: an ASP.NET Core project created with dotnet new web, running Agents SDK 1.9.205 on .NET 10. The agent was finished, and I added a middleware that answers over-long messages before they reach it. It implements IMiddleware from the Microsoft.Agents.Builder namespace, which the file imports:
using Microsoft.Agents.Builder;
using Microsoft.Agents.Core.Models;
namespace BillSplitter;
// Answers messages that are too long itself, so they never reach the agent.
public class MessageLengthLimitMiddleware(int maxLength) : IMiddleware
{
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
{
if (turnContext.Activity.Type == ActivityTypes.Message && turnContext.Activity.Text?.Length > maxLength)
{
await turnContext.SendActivityAsync($"That message is too long. Please keep it to {maxLength} characters or fewer.", cancellationToken: cancellationToken);
return;
}
await next(cancellationToken);
}
}
I registered it in Program.cs with .AddAgentMiddleware(new MessageLengthLimitMiddleware(maxLength: 100)) and built the project:
dotnet build
What I Expected
A clean build. IMiddleware is in Microsoft.Agents.Builder, the file imports that namespace, and OnTurnAsync has exactly the signature the interface asks for.
What I Got
Restore complete (0.3s)
BillSplitter net10.0 failed with 1 error(s) (0.3s)
/Users/you/source/BillSplitter/MessageLengthLimitMiddleware.cs(7,60): error CS0104: 'IMiddleware' is an ambiguous reference between 'Microsoft.Agents.Builder.IMiddleware' and 'Microsoft.AspNetCore.Http.IMiddleware'
Build failed with 1 error(s) in 1.3s

Line 7, column 60 is the IMiddleware after the colon in the class declaration.
What I Checked
Where the second IMiddleware came from. My file never mentions Microsoft.AspNetCore.Http. New .NET projects have implicit usings turned on: the SDK generates a file of global using directives in the obj folder, and they apply to every file in the project. Mine had this:
// <auto-generated/>
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Routing;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Net.Http.Json;
global using System.Threading;
global using System.Threading.Tasks;
The third line, global using Microsoft.AspNetCore.Http;, puts ASP.NET Core's HTTP namespace in scope in my middleware file.
Microsoft's documentation on implicit usings. The .NET project SDKs page lists the namespaces each SDK imports. For Microsoft.NET.Sdk.Web, the SDK of every ASP.NET Core project, the list includes Microsoft.AspNetCore.Http.
What ASP.NET Core's IMiddleware is. ASP.NET Core documents an IMiddleware interface of its own, in Microsoft.AspNetCore.Http, for factory-based HTTP middleware: it has an InvokeAsync(HttpContext, RequestDelegate) method and handles web requests. It has nothing to do with the Agents SDK's IMiddleware, which handles activities; the two only share a name.
Microsoft's documentation on CS0104. The C# reference says the compiler reports CS0104 when the same name appears in two or more namespaces imported by using directives, and suggests a fully qualified name or a namespace alias.
Why It Happens
Two namespaces in scope each contain a type called IMiddleware: Microsoft.Agents.Builder, which I imported for the Agents SDK, and Microsoft.AspNetCore.Http, which the web SDK imports for every file. Neither import outranks the other, so the compiler can't choose. Any Agents SDK middleware in an ASP.NET Core project with implicit usings, the default for new projects, runs into this as soon as it writes IMiddleware without its namespace.
The Fix
Add a using alias for the Agents SDK's interface to the middleware's file, with the other using directives:
using Microsoft.Agents.Builder;
using Microsoft.Agents.Core.Models;
using IMiddleware = Microsoft.Agents.Builder.IMiddleware;
An alias names exactly one type, and the compiler looks at a file's aliases before its imported namespaces. So in this file, IMiddleware now means the Agents SDK's interface, and the class declaration stays as it was.
Other Ways to Fix It
Both of these also built cleanly when I tried them:
-
Write the full name in the class declaration,
public class MessageLengthLimitMiddleware(int maxLength) : Microsoft.Agents.Builder.IMiddleware, without the alias. That's the simplest choice when the name appears only once. -
Remove the implicit using for the whole project, with an MSBuild
Usingitem, as Microsoft documents for taking a namespace out of the implicit set:BillSplitter.csproj<ItemGroup><Using Remove="Microsoft.AspNetCore.Http" /></ItemGroup>After the next build,
Microsoft.AspNetCore.Httpis gone from the generated global usings file. It affects every file in the project, though, so any file that uses ASP.NET Core's HTTP types, such asHttpContextorResults, then needs its ownusing. The alias changes one file and nothing else.
After the Fix
Restore complete (0.4s)
BillSplitter net10.0 succeeded (0.8s) → bin/Debug/net10.0/BillSplitter.dll
Build succeeded in 2.0s

The build succeeded with no errors and no warnings. When I ran the agent, the middleware answered a 130-character message with That message is too long. Please keep it to 100 characters or fewer., and the agent itself never saw it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Removing using Microsoft.Agents.Builder; to make the error go away | ITurnContext, NextDelegate and the other SDK types disappear with it | Keep it and add the alias |
Aliasing the wrong interface, using IMiddleware = Microsoft.AspNetCore.Http.IMiddleware; | The class must then implement InvokeAsync(HttpContext, RequestDelegate), and AddAgentMiddleware won't accept it | Alias Microsoft.Agents.Builder.IMiddleware |
| Turning off implicit usings for the whole project | Every file loses System, System.Linq and the rest | Add the alias to the one file that needs it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The same error in another middleware file | An alias only applies to the file it's in | Add it there too, or declare a global using alias once for the project |
CS0104 for another name, such as RouteBuilder | That type also exists in two imported namespaces | Alias or fully qualify it the same way, as in the RouteBuilder fix |
CS0535, the class doesn't implement a member | OnTurnAsync doesn't match the interface | Use Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default) |
FAQ
Can I make the alias apply to every file in the project?
Yes. Write global using IMiddleware = Microsoft.Agents.Builder.IMiddleware; in any one file, and it applies to all of them. With one middleware class, the alias in its own file is enough.
Why didn't Program.cs fail too?
Program.cs never writes the name IMiddleware. It creates the middleware with new MessageLengthLimitMiddleware(...), and the error only appears where the ambiguous name is used.
Is this a bug in the Agents SDK?
No. Both interfaces are legitimate, in different libraries; they collide only because the web SDK imports ASP.NET Core's HTTP namespace into every file. Any two libraries with a type of the same name do the same.
Conclusion
error CS0104: 'IMiddleware' is an ambiguous reference between 'Microsoft.Agents.Builder.IMiddleware' and 'Microsoft.AspNetCore.Http.IMiddleware' means the Agents SDK's middleware interface and ASP.NET Core's are both in scope: one through your using, the other through the web SDK's implicit usings. Add using IMiddleware = Microsoft.Agents.Builder.IMiddleware; to the middleware's file, and the build goes through.
Additional Resources
- Resolve errors and warnings related to using directives and namespace declarations — CS0104 and its fixes.
- .NET project SDKs: implicit using directives — the namespaces each SDK imports, and how to remove one.
- Factory-based middleware activation in ASP.NET Core — ASP.NET Core's own
IMiddlewareinterface. - IMiddleware interface (Microsoft 365 Agents SDK) — the Agents SDK's middleware interface.
Related
- Microsoft 365 Agents SDK Fundamentals in C# — Part 3: Turn Events, Error Handling and Middleware
- How to Add Middleware to an Agent in the Microsoft 365 Agents SDK in C#
- Fixed: "error CS0104: 'RouteBuilder' is an ambiguous reference between 'Microsoft.Agents.Builder.App.RouteBuilder' and 'Microsoft.AspNetCore.Routing.RouteBuilder'"
