0

I am using PackageReference in my .csproj file and my build works in Visual Studio (2017). I'm building with .net 4.5 I have looked at this:

(MSBuild not adding a /reference for nuget PackageReference when building with jenkins)

I was at first having the issue on our build server so I tried it locally on my computer. I do a restore and then the build (or rebuild). the error is:

(CoreCompile target) -> Properties\AssemblyInfo.cs(25,12): error CS0246: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)

And in my project I have

<ItemGroup>
<PackageReference Include="log4net">
  <Version>2.0.12</Version>
</PackageReference>

Thanks in advance.

Update

I created a simple solution/project that just includes one Nuget Package. I also created it with the latest framework in case that had any bearing on the issue. Again, it builds in VS2017 but fails using MSBuild the .csproj file is

<?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>{9A64612A-63A2-4CA5-8470-75DD978D7449}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>testNetwonNugetProject</RootNamespace>
    <AssemblyName>testNetwonNugetProject</AssemblyName>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <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' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <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="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json">
      <Version>13.0.1</Version>
    </PackageReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

I verified that on restore the files are added to the project obj folder as well as user/roaming/nuget and user/.nuget/packages. I get the same error message as above.

  • With `package.config` your project actually added a real reference with specific hint path to the DLL that corresponds to your project's target framework. With package reference, there is not concrete reference, is it? Well, if you publish your build, I am sure you will get what you're looking for https://stackoverflow.com/questions/13920146/using-msbuild-exe-to-publish-a-asp-net-mvc-4-project-with-the-cmd-line But it could be something else, looking at your error – T.S. Jul 08 '21 at 01:03
  • I am not suggesting that it is better. But I know that there are implications for build. I assume your projects are old framework type, not SDK type. If you ever worked with SDK projects, you will find out that unless you perform `dotnet publish` or simply include package references into your top projects, the dependencies are not collected in target folder as it used to be with old style projects. – T.S. Jul 08 '21 at 18:40
  • I tested on my side, but either in VS 2017 or in `Developer Command Prompt for VS 2017` by using command like `cd XXXXX` > `msbuild XXXXX.sln` worked. Perhaps you can try to `Clear All NuGet Cache(s)`(Tools > Options > NuGet Package Manager > General) and check `Allow NuGet to download missing packages`, then test again. – Tianyu Jul 21 '21 at 13:54

0 Answers0