英文:
MSBuildLocator.QueryVisualStudioInstances returns empty collection
问题
MSBuildLocator.IsRegistered
的值为false
,而MSBuildLocator.QueryVisualStudioInstances
返回一个空集合。
我阅读了一些关于互联网上的问题、答案和文章,但仍然不知道原因。我应该在我的项目中修复什么,才能从MSBuildLocator.QueryVisualStudioInstances
获得一个非空集合?
这是我的项目:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
</ItemGroup>
</Project>
这是我的代码:
using Microsoft.Build.Locator;
if (!MSBuildLocator.IsRegistered)
{
var vs2022 = MSBuildLocator.QueryVisualStudioInstances().Where(x => x.Name == "Visual Studio Community 2022").First();
MSBuildLocator.RegisterInstance(vs2022); // register the selected instance
}
英文:
I was going to analyze one of my projects and hit a wall right at the first step.
MSBuildLocator.IsRegistered
is false
and MSBuildLocator.QueryVisualStudioInstances
returns an empty collection.
I read some questions, answers and articles on the Internet and still have no answer why. What should I fix in my project to receieve a non-empty collection from MSBuildLocator.QueryVisualStudioInstances
?
Here is my project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
</ItemGroup>
</Project>
And here is my code:
using Microsoft.Build.Locator;
if (!MSBuildLocator.IsRegistered)
{
var vs2022 = MSBuildLocator.QueryVisualStudioInstances().Where(x => x.Name == "Visual Studio Community 2022").First();
MSBuildLocator.RegisterInstance(vs2022); // register the selected instance
}
答案1
得分: 1
更新:
看起来我重现了这个问题:
在这种情况下,问题出现在我的 launchsettings.json 文件中。
我这边的应用程序是 C# 控制台应用程序,工作内容如下:
{
"profiles": {
"MyConsoleApp": {
"commandName": "Project"
}
}
}
根据这个 launchsettings.json,我能够获取非空集合。
原始回答:
我认为它返回空值是因为在这种情况下它应该是空的。您对它们进行了筛选,但没有匹配项。
有关详细信息,请列出它们并检查它们是什么:
var allInstances = MSBuildLocator.QueryVisualStudioInstances().ToList();
在我这边,我没有看到任何项目的名称与 "Visual Studio Community 2022" 匹配。
我可以看到这些:
所以我这样做:
var latestVersionInstance = MSBuildLocator.QueryVisualStudioInstances().Where(x => x.Name == ".NET Core SDK").First();
然后它通过了。
英文:
Update:
It seems I reproduce the issue:
In this situation, the issue comes from my launchsettings.json.
The app on my side is C# console app, and the worked content is:
{
"profiles": {
"MyConsoleApp": {
"commandName": "Project"
}
}
}
Based on this launchsettings.json, I am able to get the collection with no-empty.
Original Answer:
I think why it returned empty is because it should be empty in this situation. You filter them, and no matches.
For details, please list all of them and check what they are:
var allInstances = MSBuildLocator.QueryVisualStudioInstances().ToList();
On my side, I didn't see any item's Name matches the "Visual Studio Community 2022".
I can see these:
So I do this:
var latestVersionInstance = MSBuildLocator.QueryVisualStudioInstances().Where(x => x.Name == ".NET Core SDK").First();
And it passed.
答案2
得分: 0
在我的情况下,原因很简单:我没有安装.NET 6 SDK。我以为只安装Visual Studio就足够了。但这是不够的。
英文:
In my case the cause was simple: I didn't install .NET 6 SDK. I was under the impression that installing Visual Studio is enough. But it is not enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论