-
Notifications
You must be signed in to change notification settings - Fork 563
Add --port option to dotnet try #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ec7ae6f
0b3b1eb
e996d69
372232b
73d2447
8dfc58f
37d4f66
3afa1ce
2f9d03d
f66fb54
6c06bb8
acb3d73
d7dc282
2f4e247
fdbc936
293b431
5562a24
9771cef
56560e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using FluentAssertions; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.NetworkInformation; | ||
| using Xunit; | ||
| namespace MLS.Agent.Tests | ||
| { | ||
| public class WebHostBuilderExtensionTests | ||
| { | ||
| [Fact] | ||
| public void If_launched_for_development_4242_is_used() | ||
| { | ||
| var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(true, null); | ||
| uri.ToString().Should().Be("http://localhost:4242/"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void If_not_launched_for_development_and_port_is_specified_it_is_used() | ||
| { | ||
| var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(false, 6000); | ||
| uri.ToString().Should().Be("https://localhost:6000/"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void If_not_launched_for_development_and_port_is_not_specified_a_free_port_is_returned() | ||
| { | ||
| var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(false, null); | ||
| uri.AbsoluteUri.Should().Match("https://localhost:*/"); | ||
| CheckIfPortIsAvailable(uri.Port).Should().BeTrue(); | ||
| } | ||
|
|
||
| private static bool CheckIfPortIsAvailable(int port) | ||
| { | ||
| // Evaluate current system tcp connections. This is the same information provided | ||
| // by the netstat command line application, just in .Net strongly-typed object | ||
| // form. We will look through the list, and if our port we would like to use | ||
| // in our TcpClient is occupied, we will set isAvailable to false. | ||
| IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); | ||
| TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); | ||
|
|
||
| return tcpConnInfoArray.FirstOrDefault(tcpi => tcpi.LocalEndPoint.Port == port) == null; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| using Clockwise; | ||
| using Microsoft.AspNetCore.Blazor.Server; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting.Server.Features; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.ResponseCompression; | ||
| using Microsoft.DotNet.Try.Markdown; | ||
|
|
@@ -182,8 +183,9 @@ public void Configure( | |
|
|
||
| if (StartupOptions.Mode == StartupMode.Try) | ||
| { | ||
| var uri = new Uri(app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can there be more than one?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are setting up in the Program.cs to use a single url, there will be only one here - https://github.com/dotnet/try/pull/246/files#diff-adcfc4b0634e7250308be781cbe69f63R146 |
||
| Clock.Current | ||
| .Schedule(_ => LaunchBrowser(browserLauncher,directoryAccessor), TimeSpan.FromSeconds(1)); | ||
| .Schedule(_ => LaunchBrowser(browserLauncher,directoryAccessor, uri), TimeSpan.FromSeconds(1)); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -204,15 +206,8 @@ private static void ConfigureForOrchestratorProxy(IApplicationBuilder app) | |
| }); | ||
| } | ||
|
|
||
| private void LaunchBrowser(IBrowserLauncher browserLauncher, IDirectoryAccessor directoryAccessor) | ||
| private void LaunchBrowser(IBrowserLauncher browserLauncher, IDirectoryAccessor directoryAccessor, Uri uri) | ||
| { | ||
| var processName = Process.GetCurrentProcess().ProcessName; | ||
|
|
||
| var uri = processName == "dotnet" || | ||
| processName == "dotnet.exe" | ||
| ? new Uri("http://localhost:4242") | ||
| : new Uri("http://localhost:5000"); | ||
|
|
||
| if (StartupOptions.Uri != null && | ||
| !StartupOptions.Uri.IsAbsoluteUri) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using MLS.Agent.CommandLine; | ||
|
|
||
| namespace MLS.Agent | ||
| { | ||
| public static class WebHostBuilderExtensions | ||
| { | ||
| public static IWebHostBuilder ConfigureUrlUsingPort(this IWebHostBuilder builder, int? port) | ||
| { | ||
| var uri = GetBrowserLaunchUri(IsLaunchedForDevelopment(), port); | ||
| return builder.UseUrls(uri.ToString()); | ||
| } | ||
|
|
||
| public static Uri GetBrowserLaunchUri(bool isLaunchedForDevelopment, int? port) | ||
| { | ||
| if (isLaunchedForDevelopment) | ||
| { | ||
| return new Uri("http://localhost:4242"); | ||
| } | ||
| else if (port.HasValue) | ||
| { | ||
| return new Uri($"https://localhost:{port}"); | ||
| } | ||
| else | ||
| { | ||
| return new Uri($"https://localhost:{GetFreePort()}"); | ||
| } | ||
| } | ||
|
|
||
| private static bool IsLaunchedForDevelopment() | ||
| { | ||
| var processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; | ||
| return processName == "dotnet" || processName == "dotnet.exe"; | ||
| } | ||
|
|
||
| private static int GetFreePort() | ||
| { | ||
| TcpListener l = new TcpListener(IPAddress.Loopback, 0); | ||
rchande marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| l.Start(); | ||
| int port = ((IPEndPoint)l.LocalEndpoint).Port; | ||
| l.Stop(); | ||
| return port; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.