.NET: Friendly error message when durable agent isn't registered (#2214)

* .NET: Friendly error message when durable agent isn't registered

* Updates

* Fix file encoding

* Add validation for durable agent proxies

* Copilot PR feedback
This commit is contained in:
Chris Gillum
2025-11-14 19:28:07 +00:00
committed by GitHub
parent c1786b38a7
commit 939c2d69f9
7 changed files with 223 additions and 4 deletions
@@ -80,7 +80,7 @@ public static class ServiceCollectionExtensions
DurableAgentsOptions options = new();
configure(options);
var agents = options.GetAgentFactories();
IReadOnlyDictionary<string, Func<IServiceProvider, AIAgent>> agents = options.GetAgentFactories();
// The agent dictionary contains the real agent factories, which is used by the agent entities.
services.AddSingleton(agents);
@@ -98,6 +98,30 @@ public static class ServiceCollectionExtensions
return options;
}
/// <summary>
/// Validates that an agent with the specified name has been registered.
/// </summary>
/// <param name="services">The service provider.</param>
/// <param name="agentName">The name of the agent to validate.</param>
/// <exception cref="InvalidOperationException">
/// Thrown when the agent dictionary is not registered in the service provider.
/// </exception>
/// <exception cref="AgentNotRegisteredException">
/// Thrown when the agent with the specified name has not been registered.
/// </exception>
internal static void ValidateAgentIsRegistered(IServiceProvider services, string agentName)
{
IReadOnlyDictionary<string, Func<IServiceProvider, AIAgent>>? agents =
services.GetService<IReadOnlyDictionary<string, Func<IServiceProvider, AIAgent>>>()
?? throw new InvalidOperationException(
$"Durable agents have not been configured. Ensure {nameof(ConfigureDurableAgents)} has been called on the service collection.");
if (!agents.ContainsKey(agentName))
{
throw new AgentNotRegisteredException(agentName);
}
}
private sealed class DefaultDataConverter : DataConverter
{
// Use durable agent options (web defaults + camel case by default) with case-insensitive matching.