aspnetcore/test/Microsoft.AspNetCore.WebSoc.../IWebHostPortExtensions.cs

30 lines
881 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.GetUris()
.Select(u => u.Port);
}
public static IEnumerable<Uri> GetUris(this IWebHost host)
{
return host.ServerFeatures.Get<IServerAddressesFeature>().Addresses
.Select(a => new Uri(a));
}
}
}