C#:我的第一个Azure DevOps管道(“ Hello,World!”)-“找不到类型或名称空间名称'System'”

时间:2019-11-30 07:45:08

标签: c# .net azure azure-devops azure-pipelines

(现在)我只想创建一个运行Helloworld.cs的Azure DevOps C#管道。

我在this Microsoft KB之后创建了Helloworld.csHelloworld.csproj

Helloworld.cs

using System;

class HelloWorld
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

Helloworld.csproj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Compile Include="Helloworld.cs" />
    </ItemGroup>
    <Target Name="Build">
        <Csc Sources="@(Compile)"/>
    </Target>
</Project>

我在this Microsoft KB ("Build, test, and deploy .NET Core apps")this one ("YAML Schema Reference")之后慢慢地整理了一个YAML文件。

当我尝试运行管道时,由于某种原因,编译器似乎对System中的Helloworld.cs名称空间声明有问题(这很奇怪)。

管道输出

Helloworld.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [d:\a\1\s\Helloworld.csproj]
Helloworld.cs(3,7): error CS0518: Predefined type 'System.Object' is not defined or imported [d:\a\1\s\Helloworld.csproj]
Helloworld.cs(5,9): error CS0518: Predefined type 'System.Void' is not defined or imported [d:\a\1\s\Helloworld.csproj]

Build FAILED.

因此,在进行故障排除的过程中,我发现了一个SO问题:

The type or namespace name 'System' could not be found

一些答案​​建议恢复 NuGet 软件包。因此,我按照Microsoft's NuGet [pipeline] task documentation添加了一个NuGet还原作为YAML的第一阶段。

YAML文件

trigger:
- master

pool:
  vmImage: 'windows-latest'

stages:
- stage: Restore_NuGet_Stage
  displayName: 'Restore NuGet Stage'
  jobs:
  - job: restore_nuget
    displayName: 'Restore NuGet'
    steps:
    - task: NuGetCommand@2
      inputs:
        command: restore
- stage: Build_Stage
  displayName: 'Build Stage'
  jobs: 
  - job: compile_cs
    displayName: 'Compile CS Files'
    steps:
    - task: DotNetCoreCLI@2
      displayName: 'Build Task'
      inputs:
        command: build
        projects: 'Helloworld.csproj'
        arguments: '--configuration Testing'

但是我仍然遇到上面发现的完全相同的namespace name 'System' could not be found错误。

要克服此错误,我必须解决什么?我以为只编译代码将是一件容易的事...

编辑-这是运行管道时显示的初步输出。列出了.Net Core version 2.158.1SDK Version 3.0.100,以防万一

Starting: Build Task
==============================================================================
Task         : .NET Core
Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version      : 2.158.1
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
C:\windows\system32\chcp.com 65001
Active code page: 65001
"C:\Program Files\dotnet\dotnet.exe" build d:\a\1\s\Helloworld.csproj --configuration Testing

Welcome to .NET Core 3.0!
---------------------
SDK Version: 3.0.100

1 个答案:

答案 0 :(得分:0)

您创建项目文件的参考已经过时了-这可以追溯到只有一个.net框架的年代,因此无需将msbuild告知目标对象。

如今,还需要一个额外的配置元素来告知msbuild您正在使用的框架:https://docs.microsoft.com/en-us/dotnet/core/tools/csproj

您将需要添加TargetFramework属性:

<PropertyGroup>
   <TargetFramework>netcoreapp2.1</TargetFramework>
 </PropertyGroup>

否则,编译器将不知道“系统”在哪里,因为dotnet core is based on metapackages

此外,构建dotnet核心项目还需要指定Sdk属性-当我使用dotnet new console生成默认的空控制台项目时,我得到:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

</Project>

在根级别注意Sdk=属性。记录在msdn

  

MSBuild 15.0引入了“项目SDK”的概念,该概念简化了使用需要导入属性和目标的软件开发套件的使用。

I stumbled on a decent-looking blog article总结了将旧的.net框架项目升级到dotnet核心所需的更改-总而言之,您的问题是由旧项目格式与新工具的混合引起的。要构建一个.Net Framework项目,我认为您将需要msbuild pipeline task而不是dotnet cli任务。