代码之家  ›  专栏  ›  技术社区  ›  Bill Greer

如何在.Net Core中显式确保还原、生成和发布设置一致?

  •  3
  • Bill Greer  · 技术社区  · 6 年前

    我运行以下命令:

     "C:\Program Files\dotnet\dotnet.exe" new console
    

    C:\Temp\myapp\myapp\myapp.csproj : warning NU1603: myapp depends on 
    Microsoft.NETCore.App (>= 2.1.0) but Microsoft.NETCore.App 2.1.0 was not found. 
    An approximate best match of Microsoft.NETCore.App 2.1.1 was resolved.
    
    C:\Temp\myapp\myapp\myapp.csproj : 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.
    

    当我运行dotnet--info时,会得到以下信息:

    .NET Core SDK (reflecting any global.json):
     Version:   2.1.301
     Commit:    59524873d6
    
     Runtime Environment:
     OS Name:     Windows
     OS Version:  10.0.17134
     OS Platform: Windows
     RID:         win10-x64
     Base Path:   C:\Program Files\dotnet\sdk\2.1.301\
    
    Host (useful for support):
    Version: 2.1.1
    Commit:  6985b9f684
    
    .NET Core SDKs installed:
     2.1.201 [C:\Program Files\dotnet\sdk]
     2.1.301 [C:\Program Files\dotnet\sdk]
    
     .NET Core runtimes installed:
     Microsoft.AspNetCore.All 2.1.1 [C:\Program 
     Files\dotnet\shared\Microsoft.AspNetCore.All]
     Microsoft.AspNetCore.App 2.1.1 [C:\Program 
     Files\dotnet\shared\Microsoft.AspNetCore.App]
     Microsoft.NETCore.App 2.0.7 [C:\Program 
     Files\dotnet\shared\Microsoft.NETCore.App]
     Microsoft.NETCore.App 2.1.1 [C:\Program 
     Files\dotnet\shared\Microsoft.NETCore.App]
    
     To install additional .NET Core runtimes or SDKs:
     https://aka.ms/dotnet-download
    

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    </Project>
    

    我在网上搜索过,似乎很多人都有同样的问题。我很好奇发生了什么事以及如何解决。我想我可以在CLI命令中更明确一些,但是我不确定如何以及应该明确什么。

    更复杂的是,我想我可以安装Visual Studio 2017,看看它是如何构建Dotnet核心控制台应用程序的。我想它会安装所有必需的依赖项,并以一种正常工作的方式为项目构建文件。

    我惊讶地看到csproj文件使用1.1作为目标框架:

    <Project Sdk="Microsoft.NET.Sdk">
     <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>netcoreapp1.1</TargetFramework>
     </PropertyGroup>
    </Project>
    

        Unable to find package Microsoft.NETCore.App with version (>= 1.1.2)
        - Found 1 version(s) in Microsoft Visual Studio Offline Packages [ 
        Nearest version: 1.0.0-rc2-3002702 ]
        - Found 0 version(s) in MyDownloads ConsoleApp1  
        C:\Users\admin\Source\Repos\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj  1   
    

    更新

    Assets file not found project.assets.json.  Run a nuget restore to create file.  
    

    nuget还原运行时没有错误,但不会创建此类文件。

    我从命令行构建了同一个项目,并创建了project.assets.json,该项目生成并运行。我的Visual Studio实例有最新更新。在我的Visual Studio实例中,哪些内容已损坏或丢失?

    1 回复  |  直到 6 年前
        1
  •  1
  •   johnny 5    6 年前

    前几天我碰到了这些问题。我解决这个问题的方法很简单,就是将我的构建配置和CPU体系结构与我发布的体系结构相匹配。

    微软在 Documentation

    它们指定首先使用预期配置生成,然后使用 no-restore

    dotnet publish --no-restore -r win10-x64
    

    编辑 我想你需要更新你的csproj文件

     <TargetFramework>netcoreapp2.1.1</TargetFramework>
    <PlatformTarget>AnyCPU</PlatformTarget>
     <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    

    您可以创建 global.json 要指定哪个SDK版本:

    {
        "sdk": { "version": "2.1.300" }
    }
    

    这期杂志将在 github