.NET: Fix duplicate Foundry AgentHost port binding - #7932
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes duplicate port binding when Foundry hosting uses AgentHost.CreateBuilder, while preserving standalone ASP.NET hosting behavior.
Changes:
- Detects AgentHost-owned listener configuration.
- Adds unit coverage for both hosting paths.
- Exercises AgentHost in live integration tests.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
ServiceCollectionExtensions.cs |
Avoids duplicate Kestrel listener registration. |
FoundryListenPortTests.cs |
Tests AgentHost and standalone registration. |
Program.cs |
Uses AgentHost for the happy-path scenario. |
HappyPathHostedAgentTests.cs |
Verifies AgentHost startup and responses. |
README.md |
Documents hosting-path coverage. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): 4e1df00b6fe2
Model: gpt-5.6-sol
Overview
The change checks for the registry instance installed by AgentHostBuilder before protocol registration, preserving the package-owned listener for ordinary ASP.NET hosts while avoiding the duplicate AgentHost listener. The tests cover both documented builder paths, repeated registration, configured ports, and real AgentHost build output. However, the selected registry is also a public standalone-host service, so its registration shape is not an exclusive host-builder marker and can suppress the only Foundry listener.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/ServiceCollectionExtensions.cs
Motivation & Context
Microsoft.Agents.AI.Foundry.Hostingadded automatic port configuration for hosted applications created withWebApplication.CreateBuilder. Applications created with the documentedAgentHost.CreateBuilderpath already receive a Kestrel listener from AgentServer. Registering both listeners on port 8088 causes every hosted container to fail during startup and the client to receive HTTP 424session_not_ready.This change restores the documented
AgentHost.CreateBuilderpath while preserving automatic port configuration for standalone ASP.NET applications.Description & Review Guide
AddFoundryResponsesnow initializes the Foundry server URL through an ASP.NET startup filter before the server reads its configured addresses. A code-configuredAgentHostBuilderlistener takes precedence over that URL and remains the only Kestrel endpoint. A standaloneWebApplicationBuilderuses the URL as its only listener. This avoids inferring the builder type from public service registrations. Unit tests cover both paths, including a standalone host with an explicitServerVersionRegistryinstance. The live hosted integration suite runs itshappy-pathscenario withAgentHost.CreateBuilder, while the remaining scenarios continue to coverWebApplication.CreateBuilder.AgentHost.CreateBuildercan start and pass the readiness probe without requiring application changes or a package downgrade. Standalone hosts continue to override the base image URL with the Foundry port.Related Issue
Fixes #7617
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.