Skip to content

Implement IConnectionEndPointFeature in Kestrel, HttpSys, and IIS#62162

Open
Copilot wants to merge 14 commits intomainfrom
copilot/fix-43786
Open

Implement IConnectionEndPointFeature in Kestrel, HttpSys, and IIS#62162
Copilot wants to merge 14 commits intomainfrom
copilot/fix-43786

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 29, 2025

Implements IConnectionEndPointFeature across multiple server implementations (Kestrel, HttpSys, and IIS), refactors connection endpoint tagging into a shared utility, and extends the feature to SignalR's HttpConnectionContext.

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

IConnectionEndPointFeature was previously only implemented in DefaultConnectionContext, which is primarily used in tests. This PR makes the feature available in production connections across Kestrel, HttpSys, and IIS, and consolidates endpoint tag logic into a shared utility.

Changes

  1. TransportConnection and TransportMultiplexedConnection — Added IConnectionEndPointFeature support by updating the code generation configuration, regenerating the feature collection code, and implementing interface methods that delegate to the existing LocalEndPoint and RemoteEndPoint properties. This covers both regular TCP connections (HTTP/1.x, HTTP/2) and multiplexed QUIC connections (HTTP/3).

  2. HttpSys (RequestContext + StandardFeatureCollection) — Added IConnectionEndPointFeature to the RequestContext partial class and StandardFeatureCollection feature lookup. The implementation constructs IPEndPoint on demand from the existing lazy-initialized IHttpConnectionFeature properties (LocalIpAddress/LocalPort and RemoteIpAddress/RemotePort).

  3. IIS (IISHttpContext) — Added IConnectionEndPointFeature to IISHttpContext, including the full fast-path feature lookup support in Features.cs (static type field, backing field, Initialize(), FastFeatureGet, FastFeatureSet, and FastEnumerable). The implementation also delegates to the existing IHttpConnectionFeature properties.

  4. src/Shared/ConnectionEndpointTags.cs (new shared file) — Extracted the connection endpoint tag logic from KestrelMetrics.cs into a shared internal utility using IConnectionEndPointFeature. Exposes two overloads:

    • AddConnectionEndpointTags(ref TagList, IFeatureCollection) — used by SignalR
    • AddConnectionEndpointTags(ref TagList, BaseConnectionContext) — used by Kestrel metrics, includes QUIC/TCP detection via MultiplexedConnectionContext

    A private AddEndpointTags helper eliminates duplication between overloads. Tags follow OTel semantic conventions (server.address, server.port as int, network.type, network.transport).

  5. KestrelMetrics.cs — Updated InitializeConnectionTags to delegate to the shared utility.

  6. DefaultHubDispatcher.cs — Updated StartActivity to include connection endpoint tags in SignalR hub method activities.

  7. HttpConnectionContext — Added IConnectionEndPointFeature to the implemented interfaces list and registered it in the feature collection, making endpoint info accessible through the standard feature interface for SignalR connections.

  8. Tests:

    • TransportConnectionFeatureCollectionTests.cs — Verifies IConnectionEndPointFeature is present in the feature collection and correctly returns/sets LocalEndPoint and RemoteEndPoint for both TransportConnection and TransportMultiplexedConnection.
    • HubConnectionHandlerTests.Activity.cs — Verifies that SignalR hub activities include server.address, server.port, network.type, and network.transport tags when endpoint information is available. Note: server.port is stored as int per OTel conventions and must be checked via Activity.TagObjects rather than Activity.Tags.
    • HttpSys/test/FunctionalTests/ConnectionEndPointFeatureTests.cs — Smoke test verifying that IConnectionEndPointFeature is available from httpContext.Features in an in-process HttpSys server and returns non-null IPEndPoint instances for both LocalEndPoint and RemoteEndPoint.
    • IIS/IIS/test/IIS.Tests/ConnectionEndPointFeatureTests.cs — Smoke test verifying that IConnectionEndPointFeature is available from ctx.Features in an in-process IIS test server and returns non-null IPEndPoint instances for both LocalEndPoint and RemoteEndPoint.

Usage

var endpointFeature = connectionContext.Features.Get<IConnectionEndPointFeature>();
var localEndpoint = endpointFeature?.LocalEndPoint;
var remoteEndpoint = endpointFeature?.RemoteEndPoint;

Notes

  • No breaking changes; existing code using direct LocalEndPoint/RemoteEndPoint properties continues to work.
  • Activity.Tags only enumerates string-valued tags. Integer-valued tags such as server.port are only accessible via Activity.TagObjects.

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copilot AI changed the title [WIP] What are we doing with IConnectionEndPointFeature Implement IConnectionEndPointFeature in Kestrel's TransportConnection May 29, 2025
Copilot AI requested a review from BrennanConroy May 29, 2025 20:43
@BrennanConroy
Copy link
Copy Markdown
Member

Looking good, can you add a test that verifies the feature is in the feature collection and has values?

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 30, 2025

Looking good, can you add a test that verifies the feature is in the feature collection and has values?

Added comprehensive tests in TransportConnectionFeatureCollectionTests.cs that verify IConnectionEndPointFeature is available in the feature collection and properly returns/sets endpoint values. The tests follow existing patterns and validate both getting and setting LocalEndPoint and RemoteEndPoint through the feature interface. (dbd9f49)

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

If you need me to access, download, or install something from one of these locations, you can either:

@BrennanConroy
Copy link
Copy Markdown
Member

Can you refactor https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs#L322 to use the IConnectionEndPointFeature and put it in the src/Shared/ folder. Then update SignalR to use the shared code; see

// See https://github.com/dotnet/aspnetcore/blob/027c60168383421750f01e427e4f749d0684bc02/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs#L308

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jun 7, 2025

Can you refactor https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs#L322 to use the IConnectionEndPointFeature and put it in the sr...

Refactored the connection endpoint logic into a shared utility in src/Shared/ConnectionEndpointTags.cs that uses IConnectionEndPointFeature. Updated KestrelMetrics.cs to use the shared code and modified SignalR's DefaultHubDispatcher to include endpoint tags via the same shared utility. The SignalR server.address tag is now uncommented and working. (6c1244f)

@BrennanConroy
Copy link
Copy Markdown
Member

Looks like there is a test failure:

Error: The given key 'network.type' was not present in the dictionary.
Stack Trace:
at System.Collections.Generic.Dictionary2.get_Item(TKey key) /_/src/Servers/Kestrel/test/Interop.FunctionalTests/Http3/Http3RequestTests.cs(106,0): at Interop.FunctionalTests.Http3.Http3RequestTests.<>c__DisplayClass2_0.<GET_Metrics_HttpProtocolAndTlsSet>b__1(CollectedMeasurement1 m)

Wonder if Http3 connections implement the feature collection differently

…ion for HTTP/3

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jun 9, 2025

Looks like there is a test failure:

Error: The given key 'network.type' was not present in the dictionary.
Stack Trace:
...

Fixed the HTTP/3 test failure by adding IConnectionEndPointFeature support to TransportMultiplexedConnection. HTTP/3 connections use MultiplexedConnectionContext instead of regular TransportConnection, so the feature wasn't available. Added feature configuration, updated generated code, and improved shared utility with fallback logic. (45fbce8)

@BrennanConroy
Copy link
Copy Markdown
Member

Looks like the test fix worked, thanks! Can you add a test for the SignalR part now? I imagine we might need to add the IConnectionEndPointFeature to HttpConnectionContext.

…ignalR endpoint tags test

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jun 9, 2025

Looks like the test fix worked, thanks! Can you add a test for the SignalR part now? I imagine we might need to add the IConnectionEndPointFeature to HttpConnectionContext.

Added IConnectionEndPointFeature implementation to HttpConnectionContext and a comprehensive test that verifies SignalR activities include endpoint tags. The test validates server.address, server.port, network.type, and network.transport tags are properly added to both connect and disconnect activities. (630a05c)

@github-actions github-actions bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Feb 10, 2026
@BrennanConroy
Copy link
Copy Markdown
Member

@copilot update the PR since it's been a while and the codebase has changed. Also double check the change to make sure it's good.

Copilot AI and others added 2 commits April 4, 2026 05:06
Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
…est compile errors

Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/0a5a6edd-af5f-4fce-96d9-3d2ed8c69a2c

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 4, 2026

@copilot update the PR since it's been a while and the codebase has changed. Also double check the change to make sure it's good.

Updated the PR:

  1. Merged with latest main - resolved the conflict in Kestrel.Core.csproj to include both our ConnectionEndpointTags.cs reference and the new Buffers.MemoryPool reference from main (af13f19).

  2. Refactored ConnectionEndpointTags.cs - extracted the duplicated endpoint tag logic into a private AddEndpointTags helper, making both public overloads cleaner and easier to read.

  3. Fixed test compile error - Activity.Tags only includes string-valued tags, so server.port (stored as int per OTel semantic conventions) must be checked via Activity.TagObjects instead of Activity.Tags. Updated the test accordingly.

@BrennanConroy
Copy link
Copy Markdown
Member

@copilot undo the change to the submodule, and is it possible to implement the feature in httpsys and iis?

Copilot AI and others added 2 commits April 5, 2026 04:20
Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 5, 2026

@copilot undo the change to the submodule, and is it possible to implement the feature in httpsys and iis?

Done in 93ed79f:

  1. Submodule reverted - restored src/submodules/googletest to d72f9c8a (main's version), undoing the accidental change from af13f19f.

  2. HttpSys - added IConnectionEndPointFeature to RequestContext (interface declaration + implementation that constructs IPEndPoint from the existing lazy-initialized IHttpConnectionFeature properties) and to StandardFeatureCollection._featureFuncLookup.

  3. IIS - added IConnectionEndPointFeature to IISHttpContext (interface declaration in FeatureCollection.cs, implementation using IHttpConnectionFeature properties, and full fast-path FastFeatureGet/FastFeatureSet/FastEnumerable support in Features.cs).

Copilot AI changed the title Implement IConnectionEndPointFeature in Kestrel's TransportConnection Implement IConnectionEndPointFeature in Kestrel, HttpSys, and IIS Apr 5, 2026
@BrennanConroy
Copy link
Copy Markdown
Member

@copilot add a couple smoke tests for this in iis and httpsys. Looks like there might be a couple tests using IHttpConnectionFeature, so can use those for inspiration.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 6, 2026

@copilot add a couple smoke tests for this in iis and httpsys. Looks like there might be a couple tests using IHttpConnectionFeature, so can use those for inspiration.

Added smoke tests in 49c4fd8:

  • HttpSys (src/Servers/HttpSys/test/FunctionalTests/ConnectionEndPointFeatureTests.cs): Request_ProvidesConnectionEndPointFeature — starts an in-process HttpSys server, retrieves IConnectionEndPointFeature from httpContext.Features, and asserts both LocalEndPoint and RemoteEndPoint are non-null IPEndPoint instances with non-zero ports.

  • IIS (src/Servers/IIS/IIS/test/IIS.Tests/ConnectionEndPointFeatureTests.cs): ProvidesLocalAndRemoteEndPoints — follows the same pattern as ConnectionIdFeatureTests, using TestServer.Create to retrieve IConnectionEndPointFeature and assert both endpoints are non-null IPEndPoint instances.

@BrennanConroy BrennanConroy marked this pull request as ready for review April 10, 2026 01:03
@BrennanConroy BrennanConroy requested a review from halter73 as a code owner April 10, 2026 01:03
Copilot AI review requested due to automatic review settings April 10, 2026 01:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds production-server support for IConnectionEndPointFeature (Kestrel, HttpSys, IIS) and centralizes OTel connection endpoint tagging so SignalR and Kestrel metrics can attach consistent server.* / network.* tags.

Changes:

  • Implement/enable IConnectionEndPointFeature in Kestrel transport connections, HttpSys RequestContext, and IIS IISHttpContext feature collections.
  • Introduce shared ConnectionEndpointTags helper and wire it into Kestrel metrics + SignalR activities.
  • Add coverage tests across Kestrel, HttpSys, IIS, and SignalR activity tagging.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.Activity.cs Adds an activity-tagging test validating endpoint/network tags on hub activities.
src/SignalR/server/Core/src/Microsoft.AspNetCore.SignalR.Core.csproj Links the new shared ConnectionEndpointTags.cs into SignalR Core.
src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs Extends StartActivity to add endpoint tags when a connection is available.
src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj Links the new shared ConnectionEndpointTags.cs into Http.Connections.
src/SignalR/common/Http.Connections/src/Internal/HttpConnectionContext.cs Registers IConnectionEndPointFeature on SignalR HttpConnectionContext.
src/Shared/ConnectionEndpointTags.cs New shared utility to add server.address, server.port, network.type, network.transport tags.
src/Servers/Kestrel/tools/CodeGenerator/TransportMultiplexedConnectionFeatureCollection.cs Updates codegen configuration to include IConnectionEndPointFeature for multiplexed connections.
src/Servers/Kestrel/tools/CodeGenerator/TransportConnectionFeatureCollection.cs Updates codegen configuration to include IConnectionEndPointFeature for transport connections.
src/Servers/Kestrel/shared/TransportMultiplexedConnection.Generated.cs Regenerated feature collection plumbing to support IConnectionEndPointFeature.
src/Servers/Kestrel/shared/TransportMultiplexedConnection.FeatureCollection.cs Implements IConnectionEndPointFeature by delegating to LocalEndPoint/RemoteEndPoint.
src/Servers/Kestrel/shared/TransportConnection.Generated.cs Regenerated feature collection plumbing to support IConnectionEndPointFeature.
src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs Implements IConnectionEndPointFeature by delegating to LocalEndPoint/RemoteEndPoint.
src/Servers/Kestrel/Core/test/TransportConnectionFeatureCollectionTests.cs New tests validating presence and get/set behavior of IConnectionEndPointFeature.
src/Servers/Kestrel/Core/src/Microsoft.AspNetCore.Server.Kestrel.Core.csproj Links the new shared ConnectionEndpointTags.cs into Kestrel Core.
src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs Replaces inline endpoint-tag logic with the shared helper.
src/Servers/IIS/IIS/test/IIS.Tests/ConnectionEndPointFeatureTests.cs Adds IIS functional smoke test for IConnectionEndPointFeature.
src/Servers/IIS/IIS/src/Core/IISHttpContext.Features.cs Adds fast-path feature lookup plumbing for IConnectionEndPointFeature.
src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs Implements IConnectionEndPointFeature on IIS via IHttpConnectionFeature values.
src/Servers/HttpSys/test/FunctionalTests/ConnectionEndPointFeatureTests.cs Adds HttpSys functional smoke test for IConnectionEndPointFeature.
src/Servers/HttpSys/src/StandardFeatureCollection.cs Registers IConnectionEndPointFeature in HttpSys standard feature lookup.
src/Servers/HttpSys/src/RequestProcessing/RequestContext.FeatureCollection.cs Implements IConnectionEndPointFeature on RequestContext via IHttpConnectionFeature values.

Comment on lines +24 to +33
var endPointFeature = httpContext.Features.Get<IConnectionEndPointFeature>();
localEndPoint = endPointFeature.LocalEndPoint;
remoteEndPoint = endPointFeature.RemoteEndPoint;
}
catch (Exception ex)
{
byte[] body = Encoding.ASCII.GetBytes(ex.ToString());
httpContext.Response.Body.Write(body, 0, body.Length);
}
return Task.FromResult(0);
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test uses Exception and Task (catch (Exception ex) / Task.FromResult) but the file doesn’t import System / System.Threading.Tasks (and doesn’t fully qualify those types). This will fail to compile; add the missing using directives or fully qualify the types used in the callback.

Copilot uses AI. Check for mistakes.
Comment on lines +28 to +30
// There isn't an easy way to detect whether QUIC is the underlying transport from a feature collection alone.
// This code assumes that a multiplexed connection is QUIC.
// Improve in the future if there are additional multiplexed connection types.
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The QUIC/multiplexed-transport comment here is misleading: this overload always passes networkTransport: "tcp", and it doesn’t attempt any QUIC detection. Please update/remove the comment (or adjust the logic) so the documented behavior matches the implementation.

Suggested change
// There isn't an easy way to detect whether QUIC is the underlying transport from a feature collection alone.
// This code assumes that a multiplexed connection is QUIC.
// Improve in the future if there are additional multiplexed connection types.
// This overload only has endpoint information from the feature collection and does not attempt
// to infer whether the underlying transport is multiplexed or QUIC. For IP endpoints, it records
// the transport as TCP.

Copilot uses AI. Check for mistakes.
using Microsoft.AspNetCore.Http.Features;

#nullable enable

Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new shared utility is declared in the global namespace. In src/Shared, shared types are normally placed in an explicit namespace (e.g., Microsoft.AspNetCore.Shared in Diagnostics/ActivityCreator.cs), which helps avoid accidental collisions and keeps call sites consistent. Consider moving this type into an appropriate shared namespace and updating call sites accordingly.

Suggested change
namespace Microsoft.AspNetCore.Shared;

Copilot uses AI. Check for mistakes.
private static readonly Type IHttpResponseTrailersFeature = typeof(global::Microsoft.AspNetCore.Http.Features.IHttpResponseTrailersFeature);
private static readonly Type IHttpResetFeature = typeof(global::Microsoft.AspNetCore.Http.Features.IHttpResetFeature);
private static readonly Type IConnectionLifetimeNotificationFeature = typeof(global::Microsoft.AspNetCore.Connections.Features.IConnectionLifetimeNotificationFeature);
private static readonly Type IConnectionEndPointFeatureType = typeof(global::Microsoft.AspNetCore.Connections.Features.IConnectionEndPointFeature);
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming nit: the static Type field for the feature is IConnectionEndPointFeatureType, but the adjacent connection feature (IConnectionLifetimeNotificationFeature) doesn’t use the Type suffix. Consider renaming for consistency with the existing feature-type field naming in this file.

Suggested change
private static readonly Type IConnectionEndPointFeatureType = typeof(global::Microsoft.AspNetCore.Connections.Features.IConnectionEndPointFeature);
private static readonly Type IConnectionEndPointFeature = typeof(global::Microsoft.AspNetCore.Connections.Features.IConnectionEndPointFeature);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Attention: Shared Code Modified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants