这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MLS.Agent.Tests/AgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private IWebHostBuilder CreateWebHostBuilder()
})
.UseTestEnvironment()
.UseStartup<Startup>()
.ConfigureUrlUsingPort(_options.Port);
.ConfigureUrlUsingPort(_options.Mode, _options.Port);

return builder;
}
Expand Down
13 changes: 7 additions & 6 deletions MLS.Agent.Tests/WebHostBuilderExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion MLS.Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static IWebHost ConstructWebHost(StartupOptions options)
})
.UseEnvironment(options.EnvironmentName)
.UseStartup<Startup>()
.ConfigureUrlUsingPort(options.Port)
.ConfigureUrlUsingPort(options.Mode, options.Port)
.Build();

return webHost;
Expand Down
5 changes: 2 additions & 3 deletions MLS.Agent/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 4 additions & 10 deletions MLS.Agent/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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);
Expand Down