a2aproject / a2aproject/a2a-dotnet

[Feat]: Refactor `A2ARouteBuilderExtensions` to leverage dependency injection

未关闭
#185 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C#
星标
262
派生
64
平均合并
5 天 2 小时
30 天内合并 PR
31

描述

### Is your feature request related to a problem? Please describe.

Currently, all methods in `A2ARouteBuilderExtensions` require explicitly passing an `ITaskManager`. This feels awkward and clutters otherwise clean .NET application code.

### Describe the solution you'd like

I’d like `A2ARouteBuilderExtensions` to resolve the `ITaskManager` from the request’s `IServiceProvider`. As far as I know, it’s already established that a server application can only host a single agent, due to restrictions around well-known documents that must be rooted. Consequently, we can safely assume only one `ITaskManager` will ever be registered—likely through a new `IServiceCollection.AddA2A` extension. This makes dependency injection the natural choice.

With this change, we could eliminate the awkward Attach pattern and instead allow handler registration directly in the agent’s constructor, resulting in cleaner, more idiomatic .NET code.

### Describe alternatives you've considered

Doing something ugly like the following:

```c#
var taskManager = new TaskManager(); // <= this should have been registered/constructured through DI instead
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
builder.Services.AddSingleton(taskManager);
builder.Services.AddSingleton();

var app = builder.Build();
app.Services.GetRequiredService(); // <- this ugly trick is required, so that my agent is actually constructured before someone hits the discovery endpoint, thus registering the TaskManager's handlers
app.MapA2A(taskManager, "/chat"); // <= this shouldn't require passing the TaskManager, and differs from 99% of .NET extensions
app.MapWellKnownAgentCard(taskManager, "/chat"); // <= this shouldn't require passing the TaskManager, and differs from 99% of .NET extensions
await app.RunAsync();
```

```c#
public class Agent
{

ITaskManager _taskManager;

public Agent(ITaskManager taskManager)
{
_taskManager = taskManager;
_taskManager.OnTaskCreated = ExecuteTaskAsync;
_taskManager.OnTaskUpdated = ExecuteTaskAsync;
_taskManager.OnMessageReceived = ProcessMessageAsync;
_taskManager.OnTaskCancelled = CancelTaskAsync;
_taskManager.OnAgentCardQuery = GetCardAsync;
}

//Omitted for brevity

}
```

### Additional context

_No response_

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

贡献指南

打开贡献指南

调研方向

Find the A2ARouteBuilderExtensions class and understand how it currently receives an ITaskManager. Look at the service registration patterns in the project, likely in extension methods for IServiceCollection. The goal is to modify the extension methods to resolve ITaskManager from IServiceProvider internally. Check for existing tests related to routing or dependency injection to ensure the refactor doesn't break existing functionality.

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp
领域
backend-api-design
Issue 类型
重构
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。