From 795f91465a35257bf5f18535905c279afeb4e0a5 Mon Sep 17 00:00:00 2001 From: Mikayla Hutchinson Date: Wed, 10 Jan 2024 16:13:38 -0500 Subject: [PATCH] Add host option to disable AssemblyLoadContext A host can disable the use of AssemblyLoadContext by returning a value of "true" or "1" when GetHostOption is called with the option name "DisableAssemblyLoadContext". This is an escape hatch for hosts that have issues with the ALC behavior, allowing them to opt out until the underlying issues can be fixed. --- .../Mono.TextTemplating/CompiledTemplate.cs | 6 +++++- .../Mono.TextTemplating/HostOptions.cs | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Mono.TextTemplating/Mono.TextTemplating/HostOptions.cs diff --git a/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs b/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs index 97ba0f8..0d6635a 100644 --- a/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs +++ b/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs @@ -85,7 +85,11 @@ TemplateProcessor CreateTemplateProcessor () // hosts are supposed to return null of they don't want to use a domain // but check for CurrentDomain too so we can optimize if they do that - if (domain == null || domain == AppDomain.CurrentDomain) { + if (domain == null || domain == AppDomain.CurrentDomain +#if FEATURE_ASSEMBLY_LOAD_CONTEXT + || host.IsAssemblyLoadContextDisabled () +#endif + ) { return new TemplateProcessor (); } diff --git a/Mono.TextTemplating/Mono.TextTemplating/HostOptions.cs b/Mono.TextTemplating/Mono.TextTemplating/HostOptions.cs new file mode 100644 index 0000000..ee43096 --- /dev/null +++ b/Mono.TextTemplating/Mono.TextTemplating/HostOptions.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Microsoft.VisualStudio.TextTemplating; + +static class HostOptionExtensions +{ + const string DisableAlcOptionName = "DisableAssemblyLoadContext"; + + static bool IsOptionTrue (this ITextTemplatingEngineHost host, string optionName) => + host.GetHostOption(optionName) is string optionVal + && (optionVal == "1" || optionVal.Equals("true", StringComparison.OrdinalIgnoreCase)); + + public static bool IsAssemblyLoadContextDisabled (this ITextTemplatingEngineHost host) => host.IsOptionTrue (DisableAlcOptionName); +} \ No newline at end of file