diff --git a/MLS.Agent.Tests/AgentService.cs b/MLS.Agent.Tests/AgentService.cs index 34f2d8d30..b36fc599a 100644 --- a/MLS.Agent.Tests/AgentService.cs +++ b/MLS.Agent.Tests/AgentService.cs @@ -70,7 +70,7 @@ private IWebHostBuilder CreateWebHostBuilder() }) .UseTestEnvironment() .UseStartup() - .ConfigureUrlUsingPort(_options.Port); + .ConfigureUrlUsingPort(_options.Mode, _options.Port); return builder; } diff --git a/MLS.Agent.Tests/WebHostBuilderExtensionTests.cs b/MLS.Agent.Tests/WebHostBuilderExtensionTests.cs index f0b33ccc2..6fe43be93 100644 --- a/MLS.Agent.Tests/WebHostBuilderExtensionTests.cs +++ b/MLS.Agent.Tests/WebHostBuilderExtensionTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using FluentAssertions; +using MLS.Agent.CommandLine; using System.Linq; using System.Net; using System.Net.NetworkInformation; @@ -11,23 +12,23 @@ namespace MLS.Agent.Tests public class WebHostBuilderExtensionTests { [Fact] - public void If_launched_for_development_4242_is_used() + public void If_not_in_try_mode_4242_is_used_and_protocol_is_http() { - var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(true, null); + var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(StartupMode.Hosted, null); uri.ToString().Should().Be("http://localhost:4242/"); } [Fact] - public void If_not_launched_for_development_and_port_is_specified_it_is_used() + public void If_in_try_mode_and_port_is_specified_it_is_used() { - var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(false, 6000); + var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(StartupMode.Try, 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() + public void If_in_try_mode_and_port_is_not_specified_a_free_port_is_returned() { - var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(false, null); + var uri = WebHostBuilderExtensions.GetBrowserLaunchUri(StartupMode.Try, null); uri.AbsoluteUri.Should().Match("https://localhost:*/"); CheckIfPortIsAvailable(uri.Port).Should().BeTrue(); } diff --git a/MLS.Agent/Program.cs b/MLS.Agent/Program.cs index b6a2cc28f..1ee37ea64 100644 --- a/MLS.Agent/Program.cs +++ b/MLS.Agent/Program.cs @@ -137,7 +137,7 @@ public static IWebHost ConstructWebHost(StartupOptions options) }) .UseEnvironment(options.EnvironmentName) .UseStartup() - .ConfigureUrlUsingPort(options.Port) + .ConfigureUrlUsingPort(options.Mode, options.Port) .Build(); return webHost; diff --git a/MLS.Agent/Properties/launchSettings.json b/MLS.Agent/Properties/launchSettings.json index 69def8824..a49b525db 100644 --- a/MLS.Agent/Properties/launchSettings.json +++ b/MLS.Agent/Properties/launchSettings.json @@ -18,12 +18,11 @@ }, "MLS.Agent": { "commandName": "Project", - "commandLineArgs": "try C:/Users/dicolomb/source/repos/Public/TryDotNet.XamU.CSharp", + "commandLineArgs": "try", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:4242" + } }, "Hosted": { "commandName": "Project", diff --git a/MLS.Agent/WebHostBuilderExtensions.cs b/MLS.Agent/WebHostBuilderExtensions.cs index 839c3e057..62a696158 100644 --- a/MLS.Agent/WebHostBuilderExtensions.cs +++ b/MLS.Agent/WebHostBuilderExtensions.cs @@ -11,15 +11,15 @@ namespace MLS.Agent { public static class WebHostBuilderExtensions { - public static IWebHostBuilder ConfigureUrlUsingPort(this IWebHostBuilder builder, int? port) + public static IWebHostBuilder ConfigureUrlUsingPort(this IWebHostBuilder builder, StartupMode mode, int? port) { - var uri = GetBrowserLaunchUri(IsLaunchedForDevelopment(), port); + var uri = GetBrowserLaunchUri(mode, port); return builder.UseUrls(uri.ToString()); } - public static Uri GetBrowserLaunchUri(bool isLaunchedForDevelopment, int? port) + public static Uri GetBrowserLaunchUri(StartupMode mode, int? port) { - if (isLaunchedForDevelopment) + if (mode != StartupMode.Try) { return new Uri("http://localhost:4242"); } @@ -33,12 +33,6 @@ public static Uri GetBrowserLaunchUri(bool isLaunchedForDevelopment, int? port) } } - 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);