Skip to content

Implement IConnectionEndPointFeature in Kestrel's TransportConnection #62162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 29, 2025

This PR implements IConnectionEndPointFeature in Kestrel's TransportConnection class to address the issue where this feature was only available in test scenarios via DefaultConnectionContext.

Background

IConnectionEndPointFeature was added as part of bedrock in #10321 but was only implemented in DefaultConnectionContext, which is primarily used by test projects. This meant that production Kestrel connections couldn't access endpoint information through the standard feature interface.

Changes

The implementation adds IConnectionEndPointFeature support to TransportConnection by:

  1. Adding the feature to code generation configuration - Updated TransportConnectionFeatureCollection.cs to include IConnectionEndPointFeature in both allFeatures and implementedFeatures arrays
  2. Regenerating feature collection code - Used the existing code generator to update TransportConnection.Generated.cs with the new feature support
  3. Implementing the feature interface - Added implementation methods in TransportConnection.FeatureCollection.cs that delegate to the existing LocalEndPoint and RemoteEndPoint properties

Usage

After this change, applications can access connection endpoints through the feature interface:

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

This maintains full compatibility with existing code that uses the direct properties while providing the standard feature interface for consistency with other ASP.NET Core components.

Impact

  • Minimal changes: Only 3 files modified with 41 additions and 3 deletions
  • No breaking changes: Existing code continues to work unchanged
  • Consistent API: Provides the same endpoint access pattern used elsewhere in ASP.NET Core
  • Production ready: Makes the feature available in real Kestrel connections, not just tests

Fixes #43786.

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:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] What are we doing with IConnectionEndPointFeature Implement IConnectionEndPointFeature in Kestrel's TransportConnection May 29, 2025
@Copilot Copilot AI requested a review from BrennanConroy May 29, 2025 20:43
Copilot finished work on behalf of BrennanConroy May 29, 2025 20:43
@BrennanConroy
Copy link
Member

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

Copy link
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:

Copilot finished work on behalf of BrennanConroy May 30, 2025 00:03
@BrennanConroy
Copy link
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

Copy link
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)

Copilot finished work on behalf of BrennanConroy June 7, 2025 03:01
@BrennanConroy
Copy link
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

Copy link
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)

Copilot finished work on behalf of BrennanConroy June 9, 2025 16:53
@BrennanConroy
Copy link
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 <[email protected]>
Copy link
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)

Copilot finished work on behalf of BrennanConroy June 9, 2025 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What are we doing with IConnectionEndPointFeature
2 participants