build error of asp.net core

2019-03-26 07:43发布

问题:

I've created a sample project using dotnet, but I get the following error when build the project:

error : The project was restored using Microsoft.NETCore.App version 2.1.0-rc1, but with current settings, version 2.1.0-preview3-26411-06 would be used instead. To resolve this issue, make sure the same settings are used for restore and for subsequent operations such as build or publish. Typically this issue can occur if the RuntimeIdentifier property is set during build or publish but not during restore.

What's the problem? I'm using VS 2017 build 15.7.0

回答1:

I had similar error message:

The project was restored using Microsoft.NETCore.App version 2.0.7, but with current settings, version 2.0.0 would be used instead. To resolve this issue, make sure the same settings are used for restore and for subsequent operations such as build or publish. Typically this issue can occur if the RuntimeIdentifier property is set during build or publish but not during restore

I added RuntimeFrameworkVersion setting to .csproj file and it fixed an issue for me:

<PropertyGroup>
   <TargetFramework>netcoreapp2.0</TargetFramework>
   <RuntimeFrameworkVersion>2.0.7</RuntimeFrameworkVersion><!--here is the fix-->
</PropertyGroup>

<ItemGroup>
   <PackageReference Update="Microsoft.NETCore.App" Version="2.0.7" />
</ItemGroup>


回答2:

Seems VS is using different .net core versions for restore/build/publish. To resolve this issue, you could add TargetLatestRuntimePatch attribute in .csproj file:

<PropertyGroup>
   <TargetFramework>netcoreapp2.0</TargetFramework>
   <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>

For details, please see this page.



回答3:

In my case, in csproj file i changed

<ItemGroup>
        <PackageReference Update="Microsoft.NETCore.App" Version="2.0.5" />
</ItemGroup>

with

<ItemGroup>
    <PackageReference Update="Microsoft.NETCore.App" Version="2.1.0" />
</ItemGroup>

and it worked



回答4:

I'v installed dot net SDK 2.2.0 and found out that this isn't the correct version and the correct one was renamed to 2.1.300 to be in sync with notnetcore app which the last version is 2.1.0. I installed 2.1.300 and everything runs correctly.



回答5:

Just because you have the latest SDK installed it doesn't mean you have the latest runtime installed. I'll never quite understand that.

Run dotnet --info

I got the following (latest installed versions only shown here).

 .NET Core SDKs installed:
   2.1.300 [C:\Program Files\dotnet\sdk]

 .NET Core runtimes installed:d\Microsoft.NETCore.App]  
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  • So I installed 2.1.1 runtime, and now dotnet --info gives me 2.1.1 as well.
  • Oh and 2.1.3 actually is 2.1.1 but they had to increment it for some reason I don't fully understand about or care about.
  • Restarted Visual Studio because it never seems to be able to keep versions in sync
  • Added the following to PropertyGroup in my .csproj file (unload project + edit)

    netcoreapp2.1 2.1.1

Now I thought we didn't need to specify this this anymore, and this csproj was just created brand new today and it didn't have a runtime version at all. Whatever we're supposed to be doing this worked for me. I also found this massive thread about versioning with 2.1.1 which I skimmed over but it seems there are complications with point releases right now so maybe this specific version is necessary.


I ended up here because of this error:

error : The project was restored using Microsoft.NETCore.App version 2.1.1, but with current settings, version 2.1.0 would be used instead. To resolve this issue, make sure the same settings are used for restore and for subsequent operations such as build or publish. Typically this issue can occur if the RuntimeIdentifier property is set during build or publish but not during restore.

Adding RuntimeFrameworkVersion was the specific fix for that that worked.

Unfortunately there's no linked article for this error message, which would be helpful.



回答6:

In my case the issue was fixed by ensuring I had two projects, with one depending on the other. One project had a RuntimeIdentifier specified in the csproj file, but the other did not. Once I ensure both had matching RuntimeIdentifiers the problem was fixed.



回答7:

The specific error I was getting was

error : NETSDK1061: The project was restored using Microsoft.NETCore.App version 2.0.5, but with current settings, version 2.1.1 would be used instead.

I had

<ItemGroup> <PackageReference Update="Microsoft.NETCore.App" Version="2.0.5" /> </ItemGroup>

Further down the file. Once I removed this and did a clean, the project built successfully.



回答8:

I have a somehow different solution, working for ASPNET 2.1, as i had problems with both building and publishing processes:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.1.0</RuntimeFrameworkVersion> --> fix publishing issues
    <PlatformTarget>AnyCPU</PlatformTarget> --> fix publishing issues
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Update="Microsoft.NETCore.App" Version="2.1.0" /> --> fix building issues
    <ProjectReference Include="..\PublicSonar.Monitor.Persistent.Json\PublicSonar.Monitor.Persistent.Json.csproj" />
  </ItemGroup>
</Project>


回答9:

Experienced the same:

 The project was restored using Microsoft.NETCore.App version 2.1.2, but with current settings, version 2.1.0 would be used instead.

Removing the explicitly set --self-contained false from the dotnet publish command seemed to do the trick for us. It defaults to the same, so why it makes a difference, I have no idea.

This was with SDK version 2.1.400.



回答10:

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <UserSecretsId>aspnet-...............245435</UserSecretsId>
    <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
    <PackageReference Update="Microsoft.NETCore.App" Version="2.1.1" />
</ItemGroup>