microsoft / MSBuildLocator

An API to locate MSBuild assemblies from an installed Visual Studio location. Use this to ensure that calling the MSBuild API will use the same toolset that a build from Visual Studio or msbuild.exe would.
Other
216 stars 83 forks source link

Allow loading MSBuild into separate AssemblyLoadContext #134

Open twsouthwick opened 3 years ago

twsouthwick commented 3 years ago

Currently, the MSBuild assemblies are loaded into the default AssemblyLoadContext. However, I'm interested in being able to load it into a seprate one so I can manage its lifetime separately from the rest of the app.

There are two ways I see this can be added:

  1. Replace the call to AssemblyLoadContext.Default to .CurrentContextualReflectionContext (which will require adding a .NET Core 3.1+ target)
#if NETCOREAPP2_1
            AssemblyLoadContext.Default.Resolving += s_registeredHandler;
#else
            var alc = AssemblyLoadContext.CurrentContextualReflectionContext ?? AssemblyLoadContext.Default;
            alc.Resolving += s_registeredHandler;
#endif
  1. Add an overload to RegisterMSBuild{xxx}(...) that takes an AssemblyLoadContext

Problems with this:

The first problem is surmountable by changing how it's tracked that MSBuild is loaded. The second issue is something I don't have an answer for.

rainersigwald commented 2 years ago

MSBuild itself has some assumptions about running in the default ALC. I don't think we had a bug for that so I filed https://github.com/dotnet/msbuild/issues/6794.

Until that is fixed, I'm not sure if there's value in changing Locator's behavior, but as soon as it's possible/supported to load MSBuild in an ALC Locator should definitely support/encourage it as well.

TomKuhn commented 6 months ago

This also gets my vote, I have requirements to maintain the lifetime of and unload msbuild from my process when done with it.