Obtaining a list of app URLs

Using IServerAddressesFeature to get a list of URLs your app is responding to

Home DailyDrop

Daily Knowledge Drop

The IServerAddressesFeature or IServer implementations can be used to get a list of URLs your application is responding to. In a landscape with a large number of applications which serve on multiple URLs, this could be leveraged to easily self-document all URLs currently in use.


Multiple urls

For the samples below, the application has been configured to use multiple urls using the UseUrls method:

builder.WebHost.UseUrls("http://*:5096;http://*:5097;http://localhost:5098");

If we had a "hello" endpoint:

app.MapGet("/hello", () =>
{
    return "world";
});

We would get a "world" response by browsing to any of the below:

  • http://localhost:5096/hello
  • http://localhost:5097/hello
  • http://localhost:5098/hello

Address list

IServerAddressesFeature

The first method to get a list of addresses is by casting the WebApplication instance to IApplicationBuilder and leveraging the ServerFeatures method available:

// app is obtained from var app = builder.Build();
// in this example, serverAddress is a free variable
var serverAddress = (app as IApplicationBuilder)
    .ServerFeatures.Get<IServerAddressesFeature>();

app.MapGet("/addresses", (context) =>
{
    // get all addresses and output
    foreach(var address in serverAddress.Addresses)
    {
        context.Response.WriteAsync($"- {address}{Environment.NewLine}");
    }

    return Task.CompletedTask;
});

The output from browsing to the above endpoint:

- http://[::]:5096
- http://[::]:5097
- http://localhost:5098

IServer

When IApplicationBuilder is not available (outside of application startup), the information is also available by injecting IServer into the required constructor or delegate:

app.MapGet("/iserver", (HttpContext context, IServer server) =>
{
    // get the address from IServer instead of IApplicationBuilder
    // otherwise everything else is the same
    var addressFeature = server.Features.Get<IServerAddressesFeature>();

    foreach (var address in addressFeature.Addresses)
    {
        context.Response.WriteAsync($"- {address}{Environment.NewLine}");
    }

    return Task.CompletedTask;
});

The output from browsing to the above endpoint:

- http://[::]:5096
- http://[::]:5097
- http://localhost:5098

Notes

We've looked at two different ways to get a list of addresses the application is serving on. For most applications, this might never be useful as most applications serve on a single address. However in the case where there are multiple addresses, this could be used to self document and easily keep track of application's addresses.


References

Server Addresses Feature
Server Addresses Feature - 2

Daily Drop 173: 03-10-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net server addresses features