Skip to main content

Fixed: "warning CS0618: 'AgentApplication.OnActivity(RouteSelector, RouteHandler, ushort, string[], bool)' is obsolete" in the Microsoft 365 Agents SDK

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

I gave a Microsoft 365 Agents SDK agent one route that catches every activity, the short way: OnActivity with a selector that always returns true. The agent worked, but the build warned CS0618. In version 1.9.205 that overload is obsolete, and the SDK's own source shows exactly what replaces it.

What I Was Doing​

I was building the Activity Inspector for Part 1 of the Microsoft 365 Agents SDK Fundamentals: an agent on .NET 10 and Agents SDK 1.9.205 that answers every activity it receives with a summary of its fields. It needs one route that wants every activity, whatever its type. A route pairs a selector, which decides whether the route wants the activity, with a handler, so I registered one in the agent's constructor with a selector that always says yes:

InspectorAgent.cs (the constructor)
public InspectorAgent(AgentApplicationOptions options, ILogger<InspectorAgent> logger) : base(options)
{
_logger = logger;

// One route whose selector says yes to every activity, so this handler sees them all.
OnActivity((turnContext, cancellationToken) => Task.FromResult(true), InspectAsync);
}

Then I built the project:

Terminal
dotnet build

What I Expected​

A clean build. OnActivity has an overload that takes a selector function, and Microsoft's documentation for AgentApplication lists custom conditions among the things a route can match.

What I Got​

The build succeeded, with a warning:

Output
Restore complete (0.3s)
ActivityInspector net10.0 succeeded with 1 warning(s) (1.9s) → bin/Debug/net10.0/ActivityInspector.dll
/Users/you/source/ActivityInspector/InspectorAgent.cs(18,9): warning CS0618: 'AgentApplication.OnActivity(RouteSelector, RouteHandler, ushort, string[], bool)' is obsolete: 'This method is deprecated use AddRoute(Route) or AddRoute(RouteSelector, RouteHandler, bool, ushort, string[], bool) instead.'

Build succeeded with 1 warning(s) in 2.6s

macOS-style terminal window: dotnet build of ActivityInspector succeeds with 1 warning, warning CS0618 at InspectorAgent.cs line 18, 'AgentApplication.OnActivity(RouteSelector, RouteHandler, ushort, string[], bool)' is obsolete: 'This method is deprecated use AddRoute(Route) or AddRoute(RouteSelector, RouteHandler, bool, ushort, string[], bool) instead.'

The agent ran and answered every activity, but a warning about an obsolete API is a warning I don't want in a project that's meant to last.

What I Checked​

What CS0618 means. Microsoft's C# reference describes CS0618 as 'member' is obsolete: 'text': the member is marked with the Obsolete attribute, and the text is whatever its authors wrote there. Here the text names the replacement: AddRoute(Route), or AddRoute with the selector, the handler and four more parameters.

Microsoft Learn. The AgentApplication in Microsoft 365 Agents SDK page registers its C# routes with OnActivity(ActivityTypes.Message, ...), the overload that takes an activity type string. It doesn't show the selector overload or AddRoute.

The SDK's own documentation. The package ships XML documentation with every method. For AddRoute(RouteSelector, RouteHandler, ...) it says: Developers won't typically need to call this method directly as it's used internally by all of the fluent interfaces to register routes for their specific activity types. And RouteBuilder provides a fluent builder for configuring and constructing a Route instance with custom selection logic, handlers, and routing options.

The SDK's source at version 1.9.205. AgentApplication.cs in Microsoft's Agents for .NET repository, at the v1.9.205 tag, shows the obsolete overload. All it does is build a route with RouteBuilder and add it:

AgentApplication.cs (Microsoft.Agents.Builder 1.9.205)
[Obsolete("This method is deprecated use AddRoute(Route) or AddRoute(RouteSelector, RouteHandler, bool, ushort, string[], bool) instead.")]
public AgentApplication OnActivity(RouteSelector routeSelector, RouteHandler handler, ushort rank = RouteRank.Unspecified, string[] autoSignInHandlers = null, bool isAgenticOnly = false)
{
return AddRoute(
RouteBuilder.Create()
.WithSelector(routeSelector)
.WithHandler(handler)
.WithOrderRank(rank)
.WithOAuthHandlers(autoSignInHandlers)
.AsAgentic(isAgenticOnly)
.Build()
);
}

Which other route methods are obsolete. The same file marks six overloads obsolete in 1.9.205:

Obsolete overloadIts message says to use
OnActivity(RouteSelector, ...)AddRoute(Route) or AddRoute(RouteSelector, ...)
OnActivity(MultipleRouteSelector, ...)AddRoute(Route)
OnMessage(RouteSelector, ...)AddRoute(Route)
OnMessage(MultipleRouteSelector, ...)OnMessage
OnConversationUpdate(RouteSelector, ...)AddRoute(Route)
OnConversationUpdate(string[], ...)OnConversationUpdate(string)

The overloads that take a type, a text, an event name or a regular expression, such as OnActivity(ActivityTypes.Message, ...), aren't obsolete, and neither is OnEvent(RouteSelector, ...).

Why It Happens​

In 1.9.205, the SDK builds every route with RouteBuilder. The overloads that take your own selector function are now thin wrappers that build the route for you, and they're marked Obsolete, so any code that calls them gets warning CS0618. The code still works, because the wrapper does exactly what the replacement does; the warning tells you to call the replacement yourself.

The Fix​

Build the route with RouteBuilder and register it with AddRoute:

InspectorAgent.cs (the constructor)
public InspectorAgent(AgentApplicationOptions options, ILogger<InspectorAgent> logger) : base(options)
{
_logger = logger;

// One route whose selector says yes to every activity, so this handler sees them all.
AddRoute(RouteBuilder.Create()
.WithSelector((turnContext, cancellationToken) => Task.FromResult(true))
.WithHandler(InspectAsync)
.Build());
}
  • RouteBuilder.Create() starts a route.
  • .WithSelector(...) takes the same selector function you passed to OnActivity.
  • .WithHandler(...) takes the same handler.
  • .Build() produces the Route, and AddRoute registers it.

If your old call passed more arguments, each one has a builder method, as the SDK's own wrapper shows:

Old OnActivity argumentRouteBuilder method
rank.WithOrderRank(rank)
autoSignInHandlers.WithOAuthHandlers(autoSignInHandlers)
isAgenticOnly.AsAgentic(isAgenticOnly)

In an ASP.NET Core project, add one more line. My first build after this change failed with error CS0104: 'RouteBuilder' is an ambiguous reference, because a web project also imports ASP.NET Core's own RouteBuilder. An alias at the top of the file settles it:

InspectorAgent.cs (with the other using directives)
using RouteBuilder = Microsoft.Agents.Builder.App.RouteBuilder;

That second problem has its own story: Fixed: "error CS0104: 'RouteBuilder' is an ambiguous reference between 'Microsoft.Agents.Builder.App.RouteBuilder' and 'Microsoft.AspNetCore.Routing.RouteBuilder'".

After the Fix​

With AddRoute and the alias, the build has no warnings:

Output
Restore complete (0.3s)
ActivityInspector net10.0 succeeded (0.3s) → bin/Debug/net10.0/ActivityInspector.dll

Build succeeded in 1.0s

macOS-style terminal window: dotnet build of ActivityInspector succeeded with no warnings, Build succeeded in 1.0s

The agent still answers every activity it receives, as it did before.

Common Mistakes​

MistakeWhat happensDo this instead
Silencing CS0618 with #pragma warning disable or NoWarnThe code keeps calling an API its authors have moved away fromSwitch to AddRoute with RouteBuilder
Rewriting every OnActivity callUnneeded work: the type, text, event name and regular-expression overloads aren't obsoleteChange only the six obsolete overloads
Writing RouteBuilder in a web project without an aliaserror CS0104, because ASP.NET Core has a RouteBuilder tooAdd using RouteBuilder = Microsoft.Agents.Builder.App.RouteBuilder;
Dropping the old rank argument when rewritingThe route is evaluated in a different orderCarry it over with .WithOrderRank(rank)

Troubleshooting​

SymptomLikely causeFix
CS0618 for OnMessage(RouteSelector, ...) or OnConversationUpdate(RouteSelector, ...)The same deprecationThe same fix: AddRoute with RouteBuilder
CS0618 for OnConversationUpdate(string[], ...)One route for several events is deprecatedRegister one OnConversationUpdate(string, ...) per event
CS0104: 'RouteBuilder' is an ambiguous referenceA web project's implicit usings include ASP.NET Core's routing namespaceAdd the using RouteBuilder = ...; alias
The handler never runs after the changeThe route was built but never passed to AddRouteWrap the whole RouteBuilder chain in AddRoute(...)

FAQ​

Does the old code still work?​

Yes. It's a warning, not an error, and in 1.9.205 the obsolete overload builds and adds exactly the route RouteBuilder would. Microsoft's .NET documentation also notes that marking an API obsolete doesn't necessarily mean it will be removed. But the warning is the SDK telling you which API to use from now on.

Why not call AddRoute(RouteSelector, RouteHandler, ...) directly?​

The warning allows it. The SDK's documentation says developers won't typically need to call that method directly, and RouteBuilder names each option instead of relying on the position of four extra arguments, so it's the clearer of the two.

Is OnActivity(ActivityTypes.Message, ...) obsolete too?​

No. The overloads that take an activity type, a message text, an event name or a regular expression aren't marked obsolete in 1.9.205.

Conclusion​

warning CS0618: 'AgentApplication.OnActivity(RouteSelector, ...)' is obsolete means your route uses a custom selector through an overload the Microsoft 365 Agents SDK has replaced. Build the route with RouteBuilder.Create().WithSelector(...).WithHandler(...).Build(), register it with AddRoute, and, in an ASP.NET Core project, add a using alias for RouteBuilder.

Additional Resources​