I'm having an issue using a singleton after converting a Microsoft.Build console project with a separate task library project from .NET Framework 4.7.2 to .NET 5.
With the .NET Framework version, I'm able to set the singleton in the console application and read the value in the build task (Microsoft.Build.Utilities.Task).
This will not work with .NET 5.
Any thoughts on this?
Console app (also containing the Context singleton)...
class Program
{
static void Main(string[] args)
{
// Singleton to be used in the build task (visible in .NET Framework, but not on .NET 5)
Context.Instance.Id = Guid.NewGuid();
// Create project
Microsoft.Build.Evaluation.ProjectCollection projectCollection = new Microsoft.Build.Evaluation.ProjectCollection();
projectCollection.DefaultToolsVersion = "Current";
Microsoft.Build.Evaluation.Project project = projectCollection.LoadProject(@".\TestProject.proj");
// Return true if there is a value in the singleton Id, false if null.
bool success = project.Build("Target");
System.Diagnostics.Debug.WriteLine($"Context contains a value: {success}");
}
}
In a library, this task is defined.
public class TestTask : Microsoft.Build.Utilities.Task
{
public override bool Execute()
{
return MsBuildTest.Context.Instance.Id != null;
}
}