Skip to content
代码片段 群组 项目
提交 67f2c314 编辑于 作者: John Luo's avatar John Luo
浏览文件

Use port 0 to avoid port collisions during tests

上级 c88e5147
No related branches found
No related tags found
无相关合并请求
// 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));
}
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.WebSockets.Test
{
public class KestrelWebSocketHelpers
{
public static IDisposable CreateServer(ILoggerFactory loggerFactory, Func<HttpContext, Task> app, int port)
public static IDisposable CreateServer(ILoggerFactory loggerFactory, out int port, Func<HttpContext, Task> app)
{
Action<IApplicationBuilder> startup = builder =>
{
......@@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.WebSockets.Test
var configBuilder = new ConfigurationBuilder();
configBuilder.AddInMemoryCollection();
var config = configBuilder.Build();
config["server.urls"] = $"http://localhost:{port}";
config["server.urls"] = $"http://127.0.0.1:0";
var host = new WebHostBuilder()
.ConfigureServices(s => s.AddSingleton(loggerFactory))
......@@ -55,6 +55,7 @@ namespace Microsoft.AspNetCore.WebSockets.Test
.Build();
host.Start();
port = host.GetPort();
return host;
}
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册