.NET: Add ILoggerFactory and IServiceProvider to HarnessAgent constructor#6273
.NET: Add ILoggerFactory and IServiceProvider to HarnessAgent constructor#6273westey-m wants to merge 2 commits into
Conversation
Add optional ILoggerFactory and IServiceProvider parameters to the HarnessAgent constructor and AsHarnessAgent extension method, passing them to all downstream components that accept them: - FunctionInvokingChatClient (via UseFunctionInvocation) - CompactionProvider - AgentSkillsProvider - ChatClientAgent (via BuildAIAgent) - AIAgentBuilder.Build() Closes microsoft#6103 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 82%
✓ Correctness
The PR correctly plumbs optional ILoggerFactory and IServiceProvider parameters through the HarnessAgent constructor and AsHarnessAgent extension method to all downstream components. All verified API signatures (CompactionProvider, AgentSkillsProvider, BuildAIAgent, AIAgentBuilder.Build) match the parameter passing in the diff. The changes are non-breaking since all new parameters are optional with null defaults.
✓ Security Reliability
This PR adds optional ILoggerFactory and IServiceProvider parameters to the HarnessAgent constructor and AsHarnessAgent extension method, passing them to downstream components. All downstream APIs (CompactionProvider, AgentSkillsProvider, UseFunctionInvocation, BuildAIAgent, AIAgentBuilder.Build) already accept these as nullable optional parameters with safe null-fallback behavior (e.g., NullLoggerFactory.Instance, EmptyServiceProvider.Instance). The change is non-breaking and introduces no security or reliability concerns.
✓ Test Coverage
The new tests verify that the constructor and extension method don't throw when ILoggerFactory and IServiceProvider are provided, which is consistent with the 'Constructor_SucceedsWhenOptionsIsNull' pattern. However, the test file extensively uses GetService<>() to verify internal wiring (e.g., verifying FunctionInvokingChatClient configuration, context providers present). The new tests only assert Assert.NotNull(agent) without verifying that the logerFactory/services are actually threaded through to downstream components. Additionally, all tests use CreateAllDisabledOptions() which disables AgentSkillsProvider—one of the documented consumers of loggerFactory—so that code path is never exercised with a non-null logerFactory.
✓ Design Approach
I did not find a design-approach issue in the changed harness plumbing. The new parameters are threaded into the relevant builder/agent seams that already accept them (
AIAgentBuilder.Build(IServiceProvider?),ChatClientBuilderExtensions.BuildAIAgent(..., ILoggerFactory?, IServiceProvider?), andChatClientAgent(..., ILoggerFactory?, IServiceProvider?)), and the updated flow is consistent with how the existing agent stack resolves logging and DI.
Automated review by westey-m's agents
There was a problem hiding this comment.
Pull request overview
This PR extends the .NET HarnessAgent creation APIs to accept optional ILoggerFactory and IServiceProvider, and threads those values through the internal build pipeline so downstream components can use logging and DI when available.
Changes:
- Added optional
ILoggerFactoryandIServiceProviderparameters toHarnessAgentconstruction and internal build helpers. - Updated
AsHarnessAgentextension method to accept and forward the same parameters. - Added unit tests covering construction paths with the new parameters.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs | Adds ILoggerFactory/IServiceProvider parameters and forwards them to builder + context providers. |
| dotnet/src/Microsoft.Agents.AI.Harness/ChatClientHarnessExtensions.cs | Extends AsHarnessAgent to accept and forward the new optional parameters. |
| dotnet/tests/Microsoft.Agents.AI.Harness.UnitTests/HarnessAgentTests.cs | Adds tests for new construction/extension-method parameter combinations. |
- Add test verifying ILoggerFactory.CreateLogger() is called by downstream components (CompactionProvider, AgentSkillsProvider) - Add test verifying IServiceProvider is queried during pipeline build Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Adds optional
ILoggerFactoryandIServiceProviderparameters to theHarnessAgentconstructor andAsHarnessAgentextension method, passing them through to all downstream components that accept them.Components receiving ILoggerFactory
FunctionInvokingChatClient(viaUseFunctionInvocation)CompactionProviderAgentSkillsProviderChatClientAgent(viaBuildAIAgent)Components receiving IServiceProvider
ChatClientAgent(viaBuildAIAgent)AIAgentBuilder.Build()Changes
HarnessAgent.cs— added parameters to constructor and internal build methodsChatClientHarnessExtensions.cs— added parameters toAsHarnessAgentHarnessAgentTests.cs— added unit tests for new parametersBoth parameters are optional with
nulldefaults, making this a non-breaking change.Closes #6103