Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit c1fff96

Browse files
author
Mike Lorbetske
committed
Update launch settings for ApplicationUrl handling
1 parent 56a56aa commit c1fff96

File tree

5 files changed

+117
-5
lines changed

5 files changed

+117
-5
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />
3+
4+
<PropertyGroup>
5+
<OutputType>Exe</OutputType>
6+
<TargetFramework>netcoreapp2.1</TargetFramework>
7+
<RuntimeIdentifiers>win7-x64;win7-x86;osx.10.12-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;ubuntu.16.10-x64;rhel.6-x64;centos.7-x64;rhel.7-x64;debian.8-x64;fedora.24-x64;opensuse.42.1-x64;alpine.3.6-x64</RuntimeIdentifiers>
8+
</PropertyGroup>
9+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace MSBuildTestApp
7+
{
8+
public class Program
9+
{
10+
public static void Main(string[] args)
11+
{
12+
var message = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
13+
Console.WriteLine(message);
14+
}
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:49850/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"First": {
12+
"commandName": "Project",
13+
"environmentVariables": {
14+
"ASPNETCORE_ENVIRONMENT": "Development",
15+
"ASPNETCORE_URLS": "http://localhost:12345/"
16+
},
17+
"applicationUrl": "http://localhost:67890/"
18+
},
19+
"Second": {
20+
"commandName": "Project",
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:54321/"
25+
}
26+
}
27+
}

src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public LaunchSettingsApplyResult TryApplySettings(JObject document, JObject mode
1515
{
1616
var config = model.ToObject<ProjectLaunchSettingsModel>();
1717

18+
if (!string.IsNullOrEmpty(config.ApplicationUrl))
19+
{
20+
command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl);
21+
}
22+
1823
//For now, ignore everything but the environment variables section
1924

2025
foreach (var entry in config.EnvironmentVariables)
@@ -24,11 +29,6 @@ public LaunchSettingsApplyResult TryApplySettings(JObject document, JObject mode
2429
command.EnvironmentVariable(entry.Key, value);
2530
}
2631

27-
if (!string.IsNullOrEmpty(config.ApplicationUrl))
28-
{
29-
command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl);
30-
}
31-
3232
return new LaunchSettingsApplyResult(true, null, config.LaunchUrl);
3333
}
3434

test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,66 @@ public void ItDefaultsToTheFirstUsableLaunchProfile()
364364
cmd.StdErr.Should().BeEmpty();
365365
}
366366

367+
[Fact]
368+
public void ItPrefersTheValueOfApplicationUrlFromEnvironmentVariablesOverTheProperty()
369+
{
370+
var testAppName = "AppWithApplicationUrlInLaunchSettings";
371+
var testInstance = TestAssets.Get(testAppName)
372+
.CreateInstance()
373+
.WithSourceFiles();
374+
375+
var testProjectDirectory = testInstance.Root.FullName;
376+
377+
new RestoreCommand()
378+
.WithWorkingDirectory(testProjectDirectory)
379+
.Execute("/p:SkipInvalidConfigurations=true")
380+
.Should().Pass();
381+
382+
new BuildCommand()
383+
.WithWorkingDirectory(testProjectDirectory)
384+
.Execute()
385+
.Should().Pass();
386+
387+
var cmd = new RunCommand()
388+
.WithWorkingDirectory(testProjectDirectory)
389+
.ExecuteWithCapturedOutput("--launch-profile First");
390+
391+
cmd.Should().Pass()
392+
.And.HaveStdOutContaining("http://localhost:12345/");
393+
394+
cmd.StdErr.Should().BeEmpty();
395+
}
396+
397+
[Fact]
398+
public void ItUsesTheValueOfApplicationUrlIfTheEnvironmentVariableIsNotSet()
399+
{
400+
var testAppName = "AppWithApplicationUrlInLaunchSettings";
401+
var testInstance = TestAssets.Get(testAppName)
402+
.CreateInstance()
403+
.WithSourceFiles();
404+
405+
var testProjectDirectory = testInstance.Root.FullName;
406+
407+
new RestoreCommand()
408+
.WithWorkingDirectory(testProjectDirectory)
409+
.Execute("/p:SkipInvalidConfigurations=true")
410+
.Should().Pass();
411+
412+
new BuildCommand()
413+
.WithWorkingDirectory(testProjectDirectory)
414+
.Execute()
415+
.Should().Pass();
416+
417+
var cmd = new RunCommand()
418+
.WithWorkingDirectory(testProjectDirectory)
419+
.ExecuteWithCapturedOutput("--launch-profile Second");
420+
421+
cmd.Should().Pass()
422+
.And.HaveStdOutContaining("http://localhost:54321/");
423+
424+
cmd.StdErr.Should().BeEmpty();
425+
}
426+
367427
[Fact]
368428
public void ItGivesAnErrorWhenTheLaunchProfileNotFound()
369429
{

0 commit comments

Comments
 (0)