From 281736979cb0c59f4f17b1e415ee59d3d6d2ccd9 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Wed, 29 Sep 2021 11:49:59 +0100 Subject: [PATCH] Update HostFactoryResolver to account for lack of application name with new host pattern Based on https://github.com/dotnet/efcore/pull/26198 The fix should go here, rather than in EF Core, because then anybody using this code will get the fixed behavior. I have tested this locally and it fixes https://github.com/dotnet/efcore/issues/26177. However tests should be added to this repo. --- .../src/HostFactoryResolver.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs b/src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs index 49ef8b69517309..d860312da271cf 100644 --- a/src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs +++ b/src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs @@ -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 @@ private static bool IsFactory(MethodInfo? factory) { 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); };