【问题标题】:Dotnet build doesn't work with newcsproj and PackageReferenceDotnet 构建不适用于 newcsproj 和 PackageReference
【发布时间】:2017-07-11 22:00:38
【问题描述】:

重现步骤:

  1. 打开 Visual Studio 2017,新建类库项目.NET 4.6.1
  2. 使用 Nuget 包管理器添加对 Newtonsoft.Json 的引用。
  3. 使用VS2017 成功构建项目。
  4. 打开命令行并从项目目录运行dotnet build

它给出了以下错误:

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

dotnet 版本:

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

编辑: 运行 dotnet restore 之前:

运行 dotnet restore 后

用文件编辑: 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; }
    }
}

【问题讨论】:

  • 请提供minimal reproducible example,作为文本而不是屏幕截图。听起来你还没有运行dotnet restore...
  • @JonSkeet 嘿乔恩。实际上我已经尝试过这样做。请看编辑。关于最小的例子,我给了你repro。截图只是为了显示版本。
  • 与其告诉我们您是如何做到的,不如向我们展示文件,以便我们可以复制/粘贴/构建。
  • 我刚刚在Visual Studio中试了一下,还不错。
  • 什么文件?你的意思是解决方案文件?

标签: c# .net


【解决方案1】:

您正在使用“旧式”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 兼容,一切都应该很好。

【讨论】:

  • PackageReference 是新 csproj 的一部分,不是吗?当我创建类库 (.NET Framework) 项目时,Visual Studio 为我创建了这个 csproj。我已在 Visual Studio 设置中选择默认使用 PackageReference。
  • @MistyK:不,这绝对是 csproj 的老风格,即使它现在支持 PackageReference。如果您想使用 .NET Core SDK,则需要“类库 (.NET Core)”,即使您将其设为 net461(根据我的示例)。
  • 啊,这很有道理。所以创建 .NET Core 类库项目并不一定意味着它建立在 .NET Core 之上,因为我仍然可以选择 .NET 框架。我很确定这只是 ASP.NET Core 项目的情况。很酷,谢谢!
【解决方案2】:

要在命令行上引用库,您需要使用 /r: 编译器选项。 例如运行csc /r:Path_to_your_DLL /t:Path_to_your_.cs_files

根据this microsoft doc,当您在 Visual Studio 中使用 nuget 时,Visual Studio 似乎会自动包含所有库,但由于您尝试使用命令行构建,您必须自己手动引用所有库.

【讨论】:

  • 我是通过nuget包管理器引用的,为什么要在命令行中手动进行呢?
  • @MistyK 更新答案
猜你喜欢
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 2017-07-04
  • 1970-01-01
相关资源
最近更新 更多