diff --git a/src/ProjectTemplates/Shared/AspNetProcess.cs b/src/ProjectTemplates/Shared/AspNetProcess.cs index e5fa728290..fe0f05fd7a 100644 --- a/src/ProjectTemplates/Shared/AspNetProcess.cs +++ b/src/ProjectTemplates/Shared/AspNetProcess.cs @@ -44,7 +44,7 @@ namespace Templates.Test.Helpers string workingDirectory, string dllPath, IDictionary environmentVariables, - bool published = true, + bool published, bool hasListeningUri = true, ILogger logger = null) { diff --git a/src/ProjectTemplates/Shared/Project.cs b/src/ProjectTemplates/Shared/Project.cs index 1fa0b57a26..6eae96dce0 100644 --- a/src/ProjectTemplates/Shared/Project.cs +++ b/src/ProjectTemplates/Shared/Project.cs @@ -147,8 +147,33 @@ namespace Templates.Test.Helpers ["ASPNETCORE_Logging__Console__IncludeScopes"] = "true", }; + var launchSettingsJson = Path.Combine(TemplateOutputDir, "Properties", "launchSettings.json"); + if (File.Exists(launchSettingsJson)) + { + // When executing "dotnet run", the launch urls specified in the app's launchSettings.json have higher precedence + // than ambient environment variables. When present, we have to edit this file to allow the application to pick random ports. + var original = File.ReadAllText(launchSettingsJson); + var updated = original.Replace( + "\"applicationUrl\": \"https://localhost:5001;http://localhost:5000\"", + $"\"applicationUrl\": \"{_urls}\""); + + if (updated == original) + { + Output.WriteLine("applicationUrl is not specified in launchSettings.json"); + } + else + { + Output.WriteLine("Updating applicationUrl in launchSettings.json"); + File.WriteAllText(launchSettingsJson, updated); + } + } + else + { + Output.WriteLine("No launchSettings.json found to update."); + } + var projectDll = Path.Combine(TemplateBuildDir, $"{ProjectName}.dll"); - return new AspNetProcess(Output, TemplateOutputDir, projectDll, environment, hasListeningUri: hasListeningUri, logger: logger); + return new AspNetProcess(Output, TemplateOutputDir, projectDll, environment, published: false, hasListeningUri: hasListeningUri, logger: logger); } internal AspNetProcess StartPublishedProjectAsync(bool hasListeningUri = true) @@ -163,7 +188,7 @@ namespace Templates.Test.Helpers }; var projectDll = $"{ProjectName}.dll"; - return new AspNetProcess(Output, TemplatePublishDir, projectDll, environment, hasListeningUri: hasListeningUri); + return new AspNetProcess(Output, TemplatePublishDir, projectDll, environment, published: true, hasListeningUri: hasListeningUri); } internal async Task RunDotNetEfCreateMigrationAsync(string migrationName)