diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj new file mode 100644 index 000000000..be02066ff --- /dev/null +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/AppWithApplicationUrlInLaunchSettings.csproj @@ -0,0 +1,8 @@ + + + + + Exe + netcoreapp2.1 + + diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs new file mode 100644 index 000000000..33322e771 --- /dev/null +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Program.cs @@ -0,0 +1,16 @@ +// 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; + +namespace MSBuildTestApp +{ + public class Program + { + public static void Main(string[] args) + { + var message = Environment.GetEnvironmentVariable("ASPNETCORE_URLS"); + Console.WriteLine(message); + } + } +} diff --git a/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json new file mode 100644 index 000000000..b61eabd57 --- /dev/null +++ b/TestAssets/TestProjects/AppWithApplicationUrlInLaunchSettings/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49850/", + "sslPort": 0 + } + }, + "profiles": { + "First": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_URLS": "http://localhost:12345/" + }, + "applicationUrl": "http://localhost:67890/" + }, + "Second": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:54321/" + } + } +} \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs b/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs index 780870494..5bb5ce85d 100644 --- a/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs +++ b/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs @@ -15,6 +15,11 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings { var config = model.ToObject(); + if (!string.IsNullOrEmpty(config.ApplicationUrl)) + { + command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl); + } + //For now, ignore everything but the environment variables section foreach (var entry in config.EnvironmentVariables) @@ -24,11 +29,6 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings command.EnvironmentVariable(entry.Key, value); } - if (!string.IsNullOrEmpty(config.ApplicationUrl)) - { - command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl); - } - return new LaunchSettingsApplyResult(true, null, config.LaunchUrl); } diff --git a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs index 35660db73..f85d7446e 100644 --- a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -364,6 +364,66 @@ namespace Microsoft.DotNet.Cli.Run.Tests cmd.StdErr.Should().BeEmpty(); } + [Fact] + public void ItPrefersTheValueOfAppUrlFromEnvVarOverTheProp() + { + var testAppName = "AppWithApplicationUrlInLaunchSettings"; + var testInstance = TestAssets.Get(testAppName) + .CreateInstance() + .WithSourceFiles(); + + var testProjectDirectory = testInstance.Root.FullName; + + new RestoreCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute("/p:SkipInvalidConfigurations=true") + .Should().Pass(); + + new BuildCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute() + .Should().Pass(); + + var cmd = new RunCommand() + .WithWorkingDirectory(testProjectDirectory) + .ExecuteWithCapturedOutput("--launch-profile First"); + + cmd.Should().Pass() + .And.HaveStdOutContaining("http://localhost:12345/"); + + cmd.StdErr.Should().BeEmpty(); + } + + [Fact] + public void ItUsesTheValueOfAppUrlIfTheEnvVarIsNotSet() + { + var testAppName = "AppWithApplicationUrlInLaunchSettings"; + var testInstance = TestAssets.Get(testAppName) + .CreateInstance() + .WithSourceFiles(); + + var testProjectDirectory = testInstance.Root.FullName; + + new RestoreCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute("/p:SkipInvalidConfigurations=true") + .Should().Pass(); + + new BuildCommand() + .WithWorkingDirectory(testProjectDirectory) + .Execute() + .Should().Pass(); + + var cmd = new RunCommand() + .WithWorkingDirectory(testProjectDirectory) + .ExecuteWithCapturedOutput("--launch-profile Second"); + + cmd.Should().Pass() + .And.HaveStdOutContaining("http://localhost:54321/"); + + cmd.StdErr.Should().BeEmpty(); + } + [Fact] public void ItGivesAnErrorWhenTheLaunchProfileNotFound() {