diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs index 9945650008..563e2a00eb 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/WebConfigTransform.cs @@ -61,23 +61,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools // replaced with backwards slashes when the application is published on a non-Windows machine var appPath = Path.Combine(".", appName).Replace("/", "\\"); var logPath = Path.Combine(configureForAzure ? @"\\?\%home%\LogFiles" : @".\logs", "stdout").Replace("/", "\\"); + RemoveLauncherArgs(aspNetCoreElement); if (!isPortable) { aspNetCoreElement.SetAttributeValue("processPath", appPath); - var arguments = (string)aspNetCoreElement.Attribute("arguments"); - - if (arguments != null) - { - const string launcherArgs = "%LAUNCHER_ARGS%"; - var position = 0; - while ((position = arguments.IndexOf(launcherArgs, position, StringComparison.OrdinalIgnoreCase)) >= 0) - { - arguments = arguments.Remove(position, launcherArgs.Length); - } - - aspNetCoreElement.SetAttributeValue("arguments", arguments.Trim()); - } } else { @@ -85,10 +73,12 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools // In Xml the order of attributes does not matter but it is nice to have // the `arguments` attribute next to the `processPath` attribute - aspNetCoreElement.Attribute("arguments")?.Remove(); + var argumentsAttribute = aspNetCoreElement.Attribute("arguments"); + argumentsAttribute?.Remove(); var attributes = aspNetCoreElement.Attributes().ToList(); var processPathIndex = attributes.FindIndex(a => a.Name.LocalName == "processPath"); - attributes.Insert(processPathIndex + 1, new XAttribute("arguments", appPath)); + attributes.Insert(processPathIndex + 1, + new XAttribute("arguments", (appPath + " " + (string)argumentsAttribute).Trim())); aspNetCoreElement.Attributes().Remove(); aspNetCoreElement.Add(attributes); @@ -113,5 +103,22 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools { element.SetAttributeValue(attributeName, (string)element.Attribute(attributeName) ?? value); } + + private static void RemoveLauncherArgs(XElement aspNetCoreElement) + { + var arguments = (string)aspNetCoreElement.Attribute("arguments"); + + if (arguments != null) + { + const string launcherArgs = "%LAUNCHER_ARGS%"; + var position = 0; + while ((position = arguments.IndexOf(launcherArgs, position, StringComparison.OrdinalIgnoreCase)) >= 0) + { + arguments = arguments.Remove(position, launcherArgs.Length); + } + + aspNetCoreElement.SetAttributeValue("arguments", arguments.Trim()); + } + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs b/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs index 1351f88fc3..bc471ba766 100644 --- a/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests/WebConfigTransformFacts.cs @@ -186,22 +186,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests aspNetCoreElement)); } - [Fact] - public void WebConfigTransform_overwrites_existing_arguments_attribute_for_portable_apps() - { - var input = WebConfigTemplate; - input.Descendants("aspNetCore").Single().SetAttributeValue("arguments", "42"); - - var aspNetCoreElement = - WebConfigTransform.Transform(input, "test.exe", configureForAzure: false, isPortable: true) - .Descendants("aspNetCore").Single(); - - Assert.True(XNode.DeepEquals( - XDocument.Parse(@"").Root, - aspNetCoreElement)); - } - [Theory] [InlineData("%LAUNCHER_ARGS%", "")] [InlineData(" %launcher_ARGS%", "")] @@ -224,6 +208,29 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools.Tests Assert.Equal(outputArguments, (string)aspNetCoreElement.Attribute("arguments")); } + [Theory] + [InlineData("", ".\\myapp.dll")] + [InlineData("%LAUNCHER_ARGS%", ".\\myapp.dll")] + [InlineData("%LAUNCHER_ARGS% %launcher_args%", ".\\myapp.dll")] + [InlineData("-my-switch", ".\\myapp.dll -my-switch")] + [InlineData(" %launcher_args% -my-switch", ".\\myapp.dll -my-switch")] + [InlineData("-my-switch %LaUnChEr_ArGs%", ".\\myapp.dll -my-switch")] + [InlineData("-switch-1 -switch-2", ".\\myapp.dll -switch-1 -switch-2")] + [InlineData("-switch-1 %LAUNCHER_ARGS% -switch-2", ".\\myapp.dll -switch-1 -switch-2")] + [InlineData("%LAUNCHER_ARGS% -switch %launcher_args%", ".\\myapp.dll -switch")] + public void WebConfigTransform_wont_override_existing_args_for_portable_apps(string inputArguments, string outputArguments) + { + var input = WebConfigTemplate; + input.Descendants("aspNetCore").Single().SetAttributeValue("arguments", inputArguments); + + var aspNetCoreElement = + WebConfigTransform.Transform(input, "myapp.dll", configureForAzure: false, isPortable: true) + .Descendants("aspNetCore").Single(); + + Assert.Equal(outputArguments, (string)aspNetCoreElement.Attribute("arguments")); + } + + private bool VerifyMissingElementCreated(params string[] elementNames) { var input = WebConfigTemplate;