mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user