簡體   English   中英

Dotnet構建不適用於newcsproj和PackageReference

[英]Dotnet build doesn't work with newcsproj and PackageReference

重現步驟:

  1. 打開Visual Studio 2017,創建新的類庫項目.NET 4.6.1
  2. 使用Nuget Package Manager添加對Newtonsoft.Json引用。
  3. 使用VS2017成功構建項目。
  4. 打開命令行並從項目目錄運行dotnet build

它給出以下錯誤:

錯誤CS0246:找不到類型或名稱空間名稱“ Newtonsoft”(您是否缺少using指令或程序集引用?)

網絡版本: 在此處輸入圖片說明

任何想法如何擺脫這個錯誤?

編輯:運行dotnet還原之前:

在此處輸入圖片說明

運行dotnet還原后

在此處輸入圖片說明

使用文件編輯: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; }
    }
}

您正在使用“舊樣式” 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兼容,並且一切都應該很好。

要在命令行上引用庫,您需要使用/ r:編譯器選項。 例如,運行csc /r:Path_to_your_DLL /t:Path_to_your_.cs_files

根據該Microsoft文檔 ,當您在Visual Studio中使用nuget時,Visual Studio似乎會自動完成包括所有庫的工作,但是由於您嘗試使用命令行進行構建,因此您必須自己手動引用所有庫。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM