From 24e471c358a81ee3c053f4a77b7c031896c20557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Sun, 5 Jul 2026 14:32:14 +0200 Subject: [PATCH] Resolve the object-model assembly by name in AppDomain wiring (Phase 6e-4b) Remove the compile-time VSTest object-model dependency from the netfx AppDomain wiring: AppDomainUtilities used typeof(TestCase).Assembly to (1) add the object-model assembly's directory to the child app-domain's resolution paths and (2) anchor the 11.0 -> current binding redirect. Both run parent-side during test source host setup, after the adapter has already loaded the object model, so the assembly is resolved by simple name from the current domain instead - returning the same (post-redirect) assembly identity the type reference did, without a compile-time reference. The only remaining mention of the object model in this file is the assembly's simple name as a string literal (used for the lookup and, formerly, by the resolver's skip-list), which is not an assembly reference and does not block dropping the package. Proven on the netfx AppDomain scenario the type reference protects: PlatformServices.Desktop.IntegrationTests (assembly-resolution-from-runsettings + deployment app-domain paths) stays green (15/15). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Utilities/AppDomainUtilities.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs index a5751f5031..f3d0b2afbc 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. #if NETFRAMEWORK @@ -6,7 +6,6 @@ using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; -using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities; @@ -18,9 +17,22 @@ internal static class AppDomainUtilities { private const string ObjectModelVersionBuiltAgainst = "11.0.0.0"; + private const string ObjectModelAssemblyName = "Microsoft.VisualStudio.TestPlatform.ObjectModel"; + private static readonly Version DefaultVersion = new(); private static readonly Version Version45 = new("4.5"); + /// + /// Resolves the loaded VSTest object-model assembly by simple name, so this AppDomain-wiring code does not + /// need a compile-time reference to it. By the time these methods run (test source host setup during + /// discovery/execution) the adapter has already loaded the object model into the current (parent) domain, + /// so its identity — including any binding redirect in effect — matches what a direct type reference resolved to. + /// + /// The object-model assembly. + private static Assembly GetObjectModelAssembly() + => AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => string.Equals(a.GetName().Name, ObjectModelAssemblyName, StringComparison.Ordinal)) + ?? Assembly.Load(ObjectModelAssemblyName); + /// /// Gets or sets the Xml Utilities instance. /// @@ -90,7 +102,7 @@ internal static string GetTargetFrameworkVersionString(string testSourcePath) var resolutionPaths = new List { - Path.GetDirectoryName(typeof(TestCase).Assembly.Location), + Path.GetDirectoryName(GetObjectModelAssembly().Location), Path.GetDirectoryName(testSourcePath), }; @@ -157,10 +169,10 @@ internal static void SetConfigurationFile(AppDomainSetup appDomainSetup, string? try { // Add redirection of the built 11.0 Object Model assembly to the current version if that is not 11.0 - string currentVersionOfObjectModel = typeof(TestCase).Assembly.GetName().Version.ToString(); + string currentVersionOfObjectModel = GetObjectModelAssembly().GetName().Version.ToString(); if (!string.Equals(currentVersionOfObjectModel, ObjectModelVersionBuiltAgainst, StringComparison.Ordinal)) { - AssemblyName assemblyName = typeof(TestCase).Assembly.GetName(); + AssemblyName assemblyName = GetObjectModelAssembly().GetName(); byte[] configurationBytes = XmlUtilities.AddAssemblyRedirection( testSourceConfigFile,