Dotnet构建不适用于newcsproj和PackageReference

时间:2017-07-11 13:42:37

标签: c# .net

重现步骤:

  1. 打开Visual Studio 2017,创建新的类库项目.NET 4.6.1
  2. 使用Nuget Package Manager添加对Newtonsoft.Json的引用。
  3. 使用VS2017成功构建项目。
  4. 打开命令行并从项目目录运行dotnet build
  5. 它出现以下错误:

      

    错误CS0246:找不到类型或命名空间名称“Newtonsoft”(您是否缺少using指令或程序集引用?)

    dotnet版本: enter image description here

    任何想法如何摆脱这个错误?

    编辑: 在运行dotnet restore之前:

    enter image description here

    运行dotnet restore

    之后

    enter image description here

    使用文件进行编辑:  ClassLibrary1.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{42A41D81-0A26-4D79-935E-6002BFAD37EB}</ProjectGuid>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>ClassLibrary1</RootNamespace>
        <AssemblyName>ClassLibrary1</AssemblyName>
        <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System.Data.DataSetExtensions" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Data" />
        <Reference Include="System.Net.Http" />
        <Reference Include="System.Xml" />
      </ItemGroup>
      <ItemGroup>
        <Compile Include="Class1.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json">
          <Version>9.0.1</Version>
        </PackageReference>
      </ItemGroup>
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    </Project>
    

    的Class1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    
    namespace ClassLibrary1
    {
        public class Class1
        {
    
            [JsonProperty]
            public string asd { get; set; }
        }
    }
    

2 个答案:

答案 0 :(得分:2)

您正在使用“旧式”msbuild项目,该项目不适用于dotnet CLI。

用以下内容替换整个项目文件:

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

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>

</Project>

现在它将与dotnet CLI兼容,并且一切都应该很好。

答案 1 :(得分:0)

要在命令行上引用库,您需要使用/ r:compiler选项。 例如,运行<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://use.fontawesome.com/82851ba7fe.js"></script> <div class="container"> <img id="eiffel" src="https://demo.cloudimg.io/width/200/s/http://sample.li/eiffel.jpg" alt="eiffel"> <i class="fa fa-check-circle fa-5x" aria-hidden="true"></i> </div>

根据this microsoft doc,当您在visual studio中使用nuget时,Visual Studio似乎会自动包含所有库,但由于您尝试使用命令行进行构建,因此您必须手动引用所有库图书馆自己。 enter image description here