26 lines
793 B
C#
26 lines
793 B
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 int GetPort(this IWebHost host)
|
|
{
|
|
return host.GetPorts().First();
|
|
}
|
|
|
|
public static IEnumerable<int> GetPorts(this IWebHost host)
|
|
{
|
|
return host.ServerFeatures.Get<IServerAddressesFeature>().Addresses
|
|
.Select(a => a.Replace("://+", "://localhost"))
|
|
.Select(a => (new Uri(a)).Port);
|
|
}
|
|
}
|
|
}
|