|
4 | 4 | // </copyright> |
5 | 5 | using System; |
6 | 6 | using System.Diagnostics; |
7 | | -using System.Net; |
8 | | -using System.Net.Sockets; |
| 7 | +using System.Linq; |
9 | 8 | using System.Threading; |
10 | 9 | using System.Threading.Tasks; |
11 | 10 | using Datadog.Demos.Util; |
12 | 11 | using Microsoft.AspNetCore.Hosting; |
13 | | -using Microsoft.Extensions.Configuration; |
| 12 | +using Microsoft.AspNetCore.Hosting.Server; |
| 13 | +using Microsoft.AspNetCore.Hosting.Server.Features; |
14 | 14 | using Microsoft.Extensions.Hosting; |
15 | 15 | using Microsoft.Extensions.Logging; |
16 | 16 |
|
@@ -56,36 +56,18 @@ public static async Task Main(string[] args) |
56 | 56 |
|
57 | 57 | using (var host = CreateHostBuilder(args).Build()) |
58 | 58 | { |
59 | | - // ASP.NET Core accepts listening url via what is set by Visual Studio |
60 | | - // (from the launchsettings.json). It could be overriden by --Urls |
61 | | - // on the command line |
62 | | - var configuration = host.Services.GetService(typeof(IConfiguration)) as IConfiguration; |
63 | | - var rootUrl = configuration["urls"]; |
64 | | - |
65 | | - // otherwise, use the default ASP.NET Core value |
66 | | - if (string.IsNullOrEmpty(rootUrl)) |
67 | | - { |
68 | | - rootUrl = "http://localhost:5000"; |
69 | | - } |
70 | | - |
71 | | - // avoid race condition in CI to find an available port |
72 | | - int port = -1; |
73 | | - if (int.TryParse(rootUrl.Substring(rootUrl.LastIndexOf(':') + 1), out port)) |
74 | | - { |
75 | | - port = GetValidPort(port, 3); |
76 | | - if (port != -1) |
77 | | - { |
78 | | - rootUrl = rootUrl.Substring(0, rootUrl.LastIndexOf(':') + 1) + port; |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - WriteLine($"Listening to {rootUrl}"); |
83 | | - |
84 | 59 | var cts = new CancellationTokenSource(); |
85 | 60 | using (var selfInvoker = new SelfInvoker(cts.Token, scenario, nbIdleThreads, _disableLogs)) |
86 | 61 | { |
87 | 62 | await host.StartAsync(); |
88 | 63 |
|
| 64 | + var server = (IServer)host.Services.GetService(typeof(IServer)); |
| 65 | + var addressFeature = server.Features.Get<IServerAddressesFeature>(); |
| 66 | + var rootUrl = addressFeature.Addresses.FirstOrDefault() ?? "http://localhost:5000"; |
| 67 | + |
| 68 | + WriteLine($"Listening to {rootUrl}"); |
| 69 | + Console.WriteLine($"##LISTENING_URL:{rootUrl}##"); |
| 70 | + |
89 | 71 | WriteLine(); |
90 | 72 | WriteLine($"Started at {DateTime.UtcNow}."); |
91 | 73 |
|
@@ -161,64 +143,6 @@ public static IHostBuilder CreateHostBuilder(string[] args) => |
161 | 143 | } |
162 | 144 | }); |
163 | 145 |
|
164 | | - public static int GetOpenPort() |
165 | | - { |
166 | | - TcpListener tcpListener = null; |
167 | | - try |
168 | | - { |
169 | | - tcpListener = new TcpListener(IPAddress.Loopback, 0); |
170 | | - tcpListener.Start(); |
171 | | - var port = ((IPEndPoint)tcpListener.LocalEndpoint).Port; |
172 | | - return port; |
173 | | - } |
174 | | - finally |
175 | | - { |
176 | | - tcpListener?.Stop(); |
177 | | - } |
178 | | - } |
179 | | - |
180 | | - private static int GetValidPort(int initialPort, int retries) |
181 | | - { |
182 | | - var port = initialPort; |
183 | | - bool isPortValid = false; |
184 | | - while (true) |
185 | | - { |
186 | | - // seems like we can't reuse a listener if it fails to start, |
187 | | - // so create a new listener each time we retry |
188 | | - var listener = new HttpListener(); |
189 | | - listener.Prefixes.Add($"http://127.0.0.1:{port}/"); |
190 | | - listener.Prefixes.Add($"http://localhost:{port}/"); |
191 | | - |
192 | | - try |
193 | | - { |
194 | | - listener.Start(); |
195 | | - |
196 | | - // success |
197 | | - isPortValid = true; |
198 | | - break; |
199 | | - } |
200 | | - catch (HttpListenerException) when (retries > 0) |
201 | | - { |
202 | | - // only catch the exception if there are retries left |
203 | | - port = GetOpenPort(); |
204 | | - retries--; |
205 | | - } |
206 | | - finally |
207 | | - { |
208 | | - listener.Close(); |
209 | | - } |
210 | | - } |
211 | | - |
212 | | - if (isPortValid) |
213 | | - { |
214 | | - return port; |
215 | | - } |
216 | | - else |
217 | | - { |
218 | | - return -1; // no valid port found |
219 | | - } |
220 | | - } |
221 | | - |
222 | 146 | private static void ParseCommandLine(string[] args, out bool disableLogs, out TimeSpan timeout, out int iterations, out Scenario scenario, out int nbIdleThreads) |
223 | 147 | { |
224 | 148 | // by default, need interactive action to exit and string.Concat scenario |
|
0 commit comments