Skip to content

Commit

Permalink
Add host option to disable AssemblyLoadContext
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mhutch committed Jan 10, 2024
1 parent 6ec6c31 commit 795f914
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

Expand Down
16 changes: 16 additions & 0 deletions Mono.TextTemplating/Mono.TextTemplating/HostOptions.cs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 795f914

Please sign in to comment.