运行Nuget Restore时出现NU1202错误 [英] Getting NU1202 error when running Nuget restore

查看:452
本文介绍了运行Nuget Restore时出现NU1202错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Framework解决方案,试图在Azure DevOps上使用管道进行设置.尝试还原软件包时出现错误:

I have a .NET Framework solution that I'm trying to set up with a pipeline on Azure DevOps. I'm getting an error when trying to restore packages though:

NU1202: Package AppCenter.Analytics.Metrics 1.1.0 is not compatible with net40 (.NETFramework,Version=v4.0). Package AppCenter.Analytics.Metrics 1.1.0 supports: netstandard1.0 (.NETStandard,Version=v1.0)

还有更多类似的东西.是的-该软件包与net40不兼容,但这无关紧要.该解决方案中有几个项目,但是最旧的.NET版本是4.5,因此我无法确定上述v4.0的来源.其他可能相关的事情:

And many more like this. It's right - that package isn't compatible with net40, but it shouldn't matter. There are several projects in the solution, but the oldest .NET version is 4.5, so I can't figure out where the v4.0 is coming from in the above. Other things that may be relevant:

  1. 内部版本输出报告5.3.1.6268的NuGet版本和16.3.2.50909的MSBuild版本.
  2. 该解决方案可以在本地正确还原和构建.
  3. 我在.sln.metaproj文件:<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>中注意到一个"v4.0".这似乎很重要,但是我不知道它是从哪里来的或如何更改它. Azure devops看不到该文件,因为它已被git忽略,但我想知道是什么原因导致该文件在那里出现4.0.
  1. The build output reports a NuGet version of 5.3.1.6268 and MSBuild version of 16.3.2.50909.
  2. The solution restores and builds correctly locally.
  3. I noticed a "v4.0" in the .sln.metaproj file: <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>. That seems relevant, but I can't figure out where it's coming from or how to change it. Azure devops doesn't see this file as it's ignored by git, but I wonder what's causing that 4.0 there.

这是pipeline.yaml中的代码段:

Here's a snippet from the pipeline.yaml:

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release 2019'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: 'select'
    vstsFeed: '#####'

这是有问题的csproj文件之一.针对多个Revit版本并查找Rhino dll的位置有些有趣的事情,但是我看不到会导致尝试为.NET v4.0安装软件包的任何事情:

And here's one of the offending csproj files. There's some funny business for targeting multiple versions of Revit, and finding the location of Rhino dlls, but I can't see anything that would lead to trying to install packages for .NET v4.0:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Rhino7DefaultInstallDir>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install', 'Path', null, RegistryView.Registry64))</Rhino7DefaultInstallDir>
    <Rhino7DebugInstallDir>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\Software\McNeel\Rhinoceros\7.0-WIP-Developer-Debug-trunk\Install', 'Path', null, RegistryView.Registry64))</Rhino7DebugInstallDir>
    <Rhino7InstallDir>$([MSBuild]::ValueOrDefault('$(Rhino7DebugInstallDir)', '$(Rhino7DefaultInstallDir)'))</Rhino7InstallDir>
    <Rhino7DefaultPluginsDir>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install', 'Default Plug-ins Folder', null, RegistryView.Registry64))</Rhino7DefaultPluginsDir>
    <Rhino7DebugPluginsDir>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\Software\McNeel\Rhinoceros\7.0-WIP-Developer-Debug-trunk\Install', 'Default Plug-ins Folder', null, RegistryView.Registry64))</Rhino7DebugPluginsDir>
    <Rhino7PluginsDir>$([MSBuild]::ValueOrDefault('$(Rhino7DebugPluginsDir)', '$(Rhino7DefaultPluginsDir)'))</Rhino7PluginsDir>
  </PropertyGroup>
  <Import Project="..\..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props" Condition="Exists('..\..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props')" />
  <PropertyGroup Condition="$(Configuration.Contains('2018'))">
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <RevitVersion>2018</RevitVersion>
  </PropertyGroup>
  <PropertyGroup Condition="$(Configuration.Contains('2019'))">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <RevitVersion>2019</RevitVersion>
  </PropertyGroup>
  <PropertyGroup Condition="$(Configuration.Contains('2020'))">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <RevitVersion>2020</RevitVersion>
  </PropertyGroup>
  <PropertyGroup>
    <!-- Common ruleset shared by all projects -->
    <CodeAnalysisRuleset>$(SolutionDir)solution.ruleset</CodeAnalysisRuleset>
  </PropertyGroup>
  <ItemGroup>
    <AdditionalFiles Include="$(SolutionDir)stylecop.json" />
  </ItemGroup>
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>9.0.30729</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{D6C256D4-B518-464F-9E68-CB282202E846}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Namespace</RootNamespace>
    <AssemblyName>AssemblyName</AssemblyName>
    <TargetFrameworkProfile />
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2018|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2018\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2018|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2018\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2019|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2019\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018; REVIT_2019</DefineConstants>
    <DebugType>portable</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2019|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2019\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018; REVIT_2019</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
    <DebugType>portable</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2020|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2020\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018; REVIT_2019; REVIT_2020</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2020|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2020\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018; REVIT_2019; REVIT_2020</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.VisualBasic" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll</HintPath>
    </Reference>
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <ItemGroup>
    ... Files ...
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="Properties\Resources.resx">
      <SubType>Designer</SubType>
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>
 ... More Files in ItemGroups ...
  <ItemGroup>
    ... Project References ...
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="AppCenter.Analytics.Metrics">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Analytics">
      <Version>2.6.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Crashes">
      <Version>2.6.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers">
      <Version>2.9.8</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="StyleCop.Analyzers">
      <Version>1.1.118</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <Choose>
    <When Condition="$(Configuration.Contains('Debug'))">
      <ItemGroup>
        <Reference Include="Eto">
          <HintPath>$(Rhino7InstallDir)Eto.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RhinoCommon">
          <HintPath>$(Rhino7InstallDir)RhinoCommon.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="Rhino.UI">
          <HintPath>$(Rhino7InstallDir)Rhino.UI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="GH_IO">
          <HintPath>$(Rhino7PluginsDir)Grasshopper\GH_IO.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="Grasshopper">
          <HintPath>$(Rhino7PluginsDir)Grasshopper\Grasshopper.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RevitAPI">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\RevitAPI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RevitAPIUI">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\RevitAPIUI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="AdWindows">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\AdWindows.dll</HintPath>
          <Private>False</Private>
        </Reference>
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <PackageReference Include="RhinoCommon" Version="7.0.19274.12465-wip" IncludeAssets="compile; build" />
        <PackageReference Include="Grasshopper" Version="7.0.19274.12465-wip" IncludeAssets="compile; build" />
        <PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).0.*" IncludeAssets="build; compile" />
      </ItemGroup>
    </Otherwise>
  </Choose>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
  </PropertyGroup>
</Project>

我一直无法弄清楚要检查什么或如何调试它.谢谢!

I haven't been able to figure out what to check for or how to debug this. Thanks!

推荐答案

我注意到.sln.metaproj文件中有一个"v4.0".我没能 弄清楚要检查什么或如何调试它.

I noticed a "v4.0" in the .sln.metaproj file. I haven't been able to figure out what to check for or how to debug this.

一些提示,可能有助于解决问题和解决问题:

#1.我认为解决方案文件夹中的.sln.metaproj和项目文件夹中的.csproj.metaproj应该从源代码管理中排除.至少不建议将这些文件发布到Azure Devops Repos.

#1. I think the .sln.metaproj in solution folder and .csproj.metaproj in project folder should be excluded from Source Control. At least these files is not recommended to publish to Azure Devops Repos.

检查它们的内容,我们可以找到类似<ProjectConfiguration Project="{xxx}" AbsolutePath="C:\Users\xxx\source\repos\...>的内容,这是本地计算机中的绝对路径.在构建管道中使用托管代理时,这些路径无效.

Check their content we can find something like <ProjectConfiguration Project="{xxx}" AbsolutePath="C:\Users\xxx\source\repos\...>, it's an absolute path in local machine. When using Hosted agent in Build Pipeline, these paths are invalid.

#2.另外,我检查了Nuget Restore任务的日志,并确认xx.xx.metaproj文件的内容不会影响nuget restore进程.因此,我认为您的问题不是由此引起的.

#2. Also, I checked the log of Nuget Restore task, and confirm the content of xx.xx.metaproj files won't affect the nuget restore process. So I think your issue didn't result from this.

#3 NU1202指示该软件包与当前项目不兼容.检查Nuget Restore任务的日志,您将看到类似以下内容的

#3 The NU1202 indicates the package is not compatible with current project. Check the log of Nuget Restore task you'll see something like this:

该错误消息将告诉我们哪个项目导致了该问题.在Devops Repos中打开xx.csproj文件以检查其内容.我假设该项目的版本目标为v4.0或其targetFrameworks之一(多目标)是v4.0.如果该项目是sdk格式的项目,则可能需要使用dotnet restore任务来为此还原软件包.

The error message will tell us which project causes the issue. Open the xx.csproj file in Devops Repos to check its content. I assume the version of the project targets v4.0 or one of its targetFrameworks(multi-targeting) is v4.0. And if that project is a sdk-format project, you may need to use dotnet restore task to restore packages for that.

更新:

请参见此处nuget restore命令将不会接收和识别构建配置.因此,在这种情况下,nuget无法访问条件为$(Configuration.Contains('xxx'))的propertyGroup.然后相当于nuget restore xx.sln => nuget恢复一个TargetFrameworkVersion为空的项目,然后nuget会认为该项目使用默认4.0.因此出现了问题.

See here, nuget restore command won't receive and recognize build configuration. So for this situation, nuget can't access the propertyGroup whose conditions is $(Configuration.Contains('xxx')). Then it is equivalent to nuget restore xx.sln=>nuget restore one project whose TargetFrameworkVersion is empty.And then nuget will consider this project uses default 4.0. So the issue occurs.

复制步骤:

1.创建一个简单的.net Framework 4.7项目,并将其添加到xx.csproj:

1.Create a simple .net framework 4.7 project, add this into xx.csproj:

    <PackageReference Include="AppCenter.Analytics.Metrics">
      <Version>1.1.0</Version>
    </PackageReference>

2.注释<!--<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>-->

3.保存所有和nuget restore xx.sln,会出现相同的问题:

3.Save all and nuget restore xx.sln, same issue occurs:

可能的解决方法:

nuget restore不会接受nuget restore xx.sln -property:Configuration=xxx之类的内容,例如nuget pack命令.因此,无论我们在哪种情况下,除非在propertyGroup中定义 valid TargetFramework版本,否则始终会发生此问题. (我想这不是您想要的,但是在这种情况下无法避免此问题)

nuget restore won't accept something like nuget restore xx.sln -property:Configuration=xxx like nuget pack command. So this issue will always occur unless we define the valid TargetFramework version in propertyGroup no matter which conditions. (I guess this is not what you want, but this issue cannot be avoided for this situation)

因此,我认为更好的选择是使用 msbuild /t:restore 而不是nuget restore.对于VS2017及更高版本,msbuild本身包含restore选项.它适用于那些使用PackageReference格式的.net fx项目.

So I think a better choice is to use msbuild /t:restore instead of nuget restore. For VS2017 and above, msbuild has contained the restore option itself. And it works for those .net fx based projects that use PackageReference format.

因此,您可以使用带有参数/t:restore的VS构建任务来还原软件包,而不是使用Nuget restore(这不适用于您的自定义csproj脚本).

So you can use VS build task with arguments /t:restore to restore the packages instead of using Nuget restore(this is not suitable for your custom csproj script).

这篇关于运行Nuget Restore时出现NU1202错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆