45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
|
|
namespace Microsoft.AspNetCore.Hosting
|
|
{
|
|
public static class IWebHostPortExtensions
|
|
{
|
|
public static string GetHost(this IWebHost host)
|
|
{
|
|
return host.GetUris().First().Host;
|
|
}
|
|
|
|
public static int GetPort(this IWebHost host)
|
|
{
|
|
return host.GetPorts().First();
|
|
}
|
|
|
|
public static int GetPort(this IWebHost host, string scheme)
|
|
{
|
|
return host.GetUris()
|
|
.Where(u => u.Scheme.Equals(scheme, StringComparison.OrdinalIgnoreCase))
|
|
.Select(u => u.Port)
|
|
.First();
|
|
}
|
|
|
|
public static IEnumerable<int> GetPorts(this IWebHost host)
|
|
{
|
|
return host.GetUris()
|
|
.Select(u => u.Port);
|
|
}
|
|
|
|
public static IEnumerable<Uri> GetUris(this IWebHost host)
|
|
{
|
|
return host.ServerFeatures.Get<IServerAddressesFeature>().Addresses
|
|
.Select(a => a.Replace("://+", "://localhost"))
|
|
.Select(a => new Uri(a));
|
|
}
|
|
}
|
|
}
|