[release/6.0] Regression Fix - Update HostFactoryResolver, set the app name via an...
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sat, 9 Oct 2021 00:20:15 +0000 (17:20 -0700)
committerGitHub <noreply@github.com>
Sat, 9 Oct 2021 00:20:15 +0000 (17:20 -0700)
src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs
src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/ApplicationNameSetFromAgrument/ApplicationNameSetFromAgrument.csproj [new file with mode: 0644]
src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/ApplicationNameSetFromAgrument/Program.cs [new file with mode: 0644]
src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/HostFactoryResolverTests.cs
src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/Microsoft.Extensions.HostFactoryResolver.Tests.csproj

index 49ef8b69517309590389a98b85d795a2c2414d67..d860312da271cf94715b06e31e418093de3f7721 100644 (file)
@@ -4,6 +4,7 @@
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
+using System.Linq;
 using System.Reflection;
 using System.Threading;
 using System.Threading.Tasks;
@@ -144,6 +145,14 @@ namespace Microsoft.Extensions.Hosting
             {
                 return args =>
                 {
+                    static bool IsApplicationNameArg(string arg)
+                        => arg.Equals("--applicationName", StringComparison.OrdinalIgnoreCase) ||
+                            arg.Equals("/applicationName", StringComparison.OrdinalIgnoreCase);
+
+                    args = args.Any(arg => IsApplicationNameArg(arg)) || assembly.FullName is null
+                        ? args
+                        : args.Concat(new[] { "--applicationName", assembly.FullName }).ToArray();
+
                     var host = hostFactory(args);
                     return GetServiceProvider(host);
                 };
diff --git a/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/ApplicationNameSetFromAgrument/ApplicationNameSetFromAgrument.csproj b/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/ApplicationNameSetFromAgrument/ApplicationNameSetFromAgrument.csproj
new file mode 100644 (file)
index 0000000..716b25a
--- /dev/null
@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFrameworks>$(NetCoreAppCurrent);net461</TargetFrameworks>
+    <EnableDefaultItems>true</EnableDefaultItems>
+    <OutputType>Exe</OutputType>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\MockHostTypes\MockHostTypes.csproj" />
+  </ItemGroup>
+
+</Project>
diff --git a/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/ApplicationNameSetFromAgrument/Program.cs b/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/ApplicationNameSetFromAgrument/Program.cs
new file mode 100644 (file)
index 0000000..f83f5c1
--- /dev/null
@@ -0,0 +1,8 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Hosting;
+
+var host = new HostBuilder().ConfigureAppConfiguration(builder => builder.AddCommandLine(args)).Build();
\ No newline at end of file
index 8f6cf79de0bf3e82e671f18536ae8d2d53452926..0e353f7f8cb7088298426e0fef31131b029b61e9 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using Microsoft.Extensions.Configuration;
 using MockHostTypes;
 using System;
 using System.Diagnostics.CodeAnalysis;
@@ -252,6 +253,17 @@ namespace Microsoft.Extensions.Hosting.Tests
             Assert.IsAssignableFrom<IServiceProvider>(factory(Array.Empty<string>()));
         }
 
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
+        public void ApplicationNameSetFromAgrument()
+        {
+            Assembly assembly = Assembly.Load("ApplicationNameSetFromAgrument");
+            var factory = HostFactoryResolver.ResolveServiceProviderFactory(assembly, s_WaitTimeout);
+            IServiceProvider? serviceProvider = factory(Array.Empty<string>());
+
+            var configuration = (IConfiguration)serviceProvider.GetService(typeof(IConfiguration));
+            Assert.Contains("ApplicationNameSetFromAgrument", configuration["applicationName"]);
+        }
+
         [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
         [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(NoSpecialEntryPointPattern.Program))]
         public void NoSpecialEntryPointPatternCanRunInParallel()
index dc808c40b7cc99456df27f50261aa2cc65dcd57b..bae78e8e4e183cab53de94c7c33a01fd85e0c533 100644 (file)
@@ -14,6 +14,7 @@
   </ItemGroup>
 
   <ItemGroup>
+    <ProjectReference Include="ApplicationNameSetFromAgrument\ApplicationNameSetFromAgrument.csproj" />
     <ProjectReference Include="BuildWebHostInvalidSignature\BuildWebHostInvalidSignature.csproj" />
     <ProjectReference Include="BuildWebHostPatternTestSite\BuildWebHostPatternTestSite.csproj" />
     <ProjectReference Include="CreateHostBuilderInvalidSignature\CreateHostBuilderInvalidSignature.csproj" />