question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RequireLocalPort extension for IEndpointConventionBuilder

See original GitHub issue

Background and Motivation

I’ve found myself in a situation where I needed endpoints responding on different ports. Mainly one for public requests and another for internal requests (health checks for example). To do this I resorted to the .RequireHost extension method and did something similar to this

app.MapHealthChecks( "/healthz" )
    .RequireHost( "*:9001" )
    .AllowAnonymous();

app.MapGet( "/", () =>
{
   // ...
} )
.RequireHost( "*:8080" );

Locally things work properly and so I was surprised when deployed behind a reverse proxy I wasn’t able to reach the public endpoint, but was able to reach the health check (internally).

Turns out that RequireHost uses the Host header, which behind a reverse proxy that may be the public host/port and not the one we’re listening to. It took me a while to figure this out and it also took me quite a while to figure out how to create a filter to look at the HttpContext.Connection.LocalPort instead.

Proposed API

In addition to better documentation on RequireHost, to clarify whoever is using it, I’d suggest having a RequireLocalPort extension. If I’m not mistaken, previous versions of UseHealthChecks had an optional port argument.

namespace Microsoft.AspNetCore.Builder;

public static class EndpointConventionBuilderLocalExtensions
{
    public static IEndpointConventionBuilder RequireLocalPort( this IEndpointConventionBuilder builder, int port )
        => builder.AddEndpointFilter( async ( context, next ) =>
        {
            if ( context.HttpContext.Connection.LocalPort != port )
            {
                return Results.NotFound();
            }

            return await next( context );
        } );
}

Usage Examples

The code above would be replaced with

app.MapHealthChecks( "/healthz" )
    .RequireLocalPort( 9001 )
    .AllowAnonymous();

app.MapGet( "/", () =>
{
   // ...
} )
.RequireLocalPort( 8080 );

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, Jan 12, 2023

Setting the host header seems like a simple solution.

I don’t have any more thoughts off the top of my head. It’s been a long time since I wrote host matcher 😄

0reactions
msftbot[bot]commented, Jan 23, 2023

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IEndpointConventionBuilder Interface
This interface is used at application startup to customize endpoints for the application. Methods. Add(Action<EndpointBuilder>). Adds the specified convention ...
Read more >
ASP.NET Core Endpoints. Add endpoint-enabled ...
Add endpoint-enabled middleware by using IEndpointRouteBuilder extension ... You can use extension method (e.g. IEndpointConventionBuilder ...
Read more >
How to build lightweight services in ASP.NET Core 6
Take advantage of the extension methods of the IEndpointConventionBuilder interface to implement lightweight services sans template or ...
Read more >
.NET and Visual Studio Code
This extension pack consists of a set of VS Code extensions that work together to provide a rich C# editing experience, AI-powered development,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found