MSBuildLocator.QueryVisualStudioInstances 返回空集合

huangapple go评论82阅读模式
英文:

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

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
  &lt;PropertyGroup&gt;
    &lt;OutputType&gt;Exe&lt;/OutputType&gt;
    &lt;TargetFramework&gt;net6.0&lt;/TargetFramework&gt;
    &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
    &lt;Nullable&gt;enable&lt;/Nullable&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;Microsoft.Build.Locator&quot; Version=&quot;1.5.5&quot; /&gt;
  &lt;/ItemGroup&gt;
&lt;/Project&gt;

And here is my code:
using Microsoft.Build.Locator;

if (!MSBuildLocator.IsRegistered)
{
    var vs2022 = MSBuildLocator.QueryVisualStudioInstances().Where(x =&gt; x.Name == &quot;Visual Studio Community 2022&quot;).First();
    MSBuildLocator.RegisterInstance(vs2022); // register the selected instance
}

答案1

得分: 1

更新:

看起来我重现了这个问题:

MSBuildLocator.QueryVisualStudioInstances 返回空集合

在这种情况下,问题出现在我的 launchsettings.json 文件中。

我这边的应用程序是 C# 控制台应用程序,工作内容如下:

{
  "profiles": {
    "MyConsoleApp": {
      "commandName": "Project"
    }
  }
}

根据这个 launchsettings.json,我能够获取非空集合。

原始回答:

我认为它返回空值是因为在这种情况下它应该是空的。您对它们进行了筛选,但没有匹配项。

有关详细信息,请列出它们并检查它们是什么:

var allInstances = MSBuildLocator.QueryVisualStudioInstances().ToList();

在我这边,我没有看到任何项目的名称与 "Visual Studio Community 2022" 匹配。

我可以看到这些:

MSBuildLocator.QueryVisualStudioInstances 返回空集合

所以我这样做:

var latestVersionInstance = MSBuildLocator.QueryVisualStudioInstances().Where(x => x.Name == ".NET Core SDK").First();

然后它通过了。

英文:

Update:

It seems I reproduce the issue:

MSBuildLocator.QueryVisualStudioInstances 返回空集合

In this situation, the issue comes from my launchsettings.json.

The app on my side is C# console app, and the worked content is:

{
  &quot;profiles&quot;: {
    &quot;MyConsoleApp&quot;: {
      &quot;commandName&quot;: &quot;Project&quot;
    }
  }
}

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:

MSBuildLocator.QueryVisualStudioInstances 返回空集合

So I do this:

var latestVersionInstance = MSBuildLocator.QueryVisualStudioInstances().Where(x =&gt; x.Name == &quot;.NET Core SDK&quot;).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.

huangapple
  • 本文由 发表于 2023年8月10日 15:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定