Closed ehsan-keshavarzian closed 2 years ago
Can you provide some more details? What is the call stack of the failure? How are you registering the instance with MSBuildLocator?
StackTrace is:
And the registration is:
I had the same exception, but I figured out that I need to call MSBuildLocator.RegisterDefaults()
earlier, see Docs. In my case I was calling RegisterDefaults
from a static class constructor, and I was using MSBuild inside the instance constructor. So apparently this is not enough to meet the "Register instance before calling MSBuild" requirement. In your case I suspect that using MSBuild from a lambda within the same method does not meet the requirement either. Once I moved the MSBuildLocator.RegisterDefaults()
call to the main method of the application, then the exception no longer occurred.
This however caused the MSBuild of .NET Core SDK 6.0.101 to be used and not the one of Visual Studio 2022. This is probably because on my system running MSBuildLocator.QueryVisualStudioInstances(VisualStudioInstanceQueryOptions.Default).ToList()
results in two instances ".NET Core SDK 6.0.101" and ".NET Core SDK 3.1.416".
Running MSBuildLocator.QueryVisualStudioInstances(new VisualStudioInstanceQueryOptions() { DiscoveryTypes = DiscoveryType.VisualStudioSetup }).ToList()
results in an empty list for me. I discovered that the VisualStudioLocationHelper
class is only included for .Net Framework 4.6, because of the following code in Microsoft.Build.Locator.csproj
<PropertyGroup Condition="'$(TargetFramework)'=='net46'">
<DefineConstants>$(DefineConstants);FEATURE_VISUALSTUDIOSETUP</DefineConstants>
</PropertyGroup>
Since I am using .Net 6 the TargetFramework is netcoreapp2.1
instead of net46
. As a workaround I did the following:
VisualStudioLocationHelper
class to your own project#if
/#endif
linesVisualStudioInstance
instance, because the constructor is internalMSBuildLocator.RegisterInstance(VisualStudioLocationHelper.GetInstances().First())
to register MSBuild instead of RegisterDefaults
;This seems to work for me, but I do not know if I will run into trouble later. Is it by-design that you cannot use the MSBuild of Visual Studio 2022 when targeting .Net 6 yourself or is this a bug? In other words, is it required to keep targeting .Net Framework instead of .Net (Core), when you want to use the MSBuild of Visual Studio 2022?
Thanks for the hint. I moved the registration to main and it works now. BTW, I'm using .NET 6.0 and not .NET Framework.
I am getting a new exception which is not related to this, so I'm going to post it somewhere else. But here it is just for the reference:
I'm using MSBuildLocator 1.4.1 with .NET 6.0 and Visual Studio 2022. But at runtime I get the following error![image](https://user-images.githubusercontent.com/8175035/149063863-66479e77-ed2b-4689-a64a-9a9d40472f0d.png)