Search code examples
c#visual-studionugetcsproj.net-framework-version

Consume a nuget in legacy non-sdk project format with CLI


I need to consume a nuget (created with dotnet pack) in some old project created with legacy non-sdk project format.

So far I see few points:

  1. I can't use dotnet add package .. with non-sdk projects as consumers.
  2. I can't use Package Manager Console (that consumes the nuget well) outside VS.

"Manual" adding a nuget package lines into csproj (to emulate what is done by Package Manager Console) like:

 ..
 <ItemGroup>
    <Reference Include="Binary1, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <HintPath>..\packages\Binary1.0.0.1\lib\netstandard2.0\Binary1.dll</HintPath>
    </Reference>
  ..

and then build solution doesn't seem working too. So, does anyone have any hints about proper consuming a nuget package in legacy non-sdk consumer csproj?


Solution

  • I can't use dotnet add package .. with non-sdk projects as consumers.

    This is of course not.

    dotnet command only support .NET Core 3.1 SDK and later versions

    enter image description here

    I can't use Package Manager Console (that consumes the nuget well) outside VS.

    Yes, this is the feature of VS. Outside, command and PowerShell is available.


    The method you are using is dll reference using. From the format it seems correct. Will issue occurs during building this project?

    Ensure that the <HintPath> is correctly relative to your project file. If the path is incorrect, the build process won't be able to find the referenced assembly. And make sure it and it's dependencies exist.

    By the way, if you use some common nuget package instead of using dll directly, we always using packagereference or packages.config.

    In net framework, Default method is packages.config

    .NET Framework projects support PackageReference, but currently default to packages.config. To use PackageReference, migrate the dependencies from packages.config into your project file, then remove packages.config.

    Setting default package management place is here:

    enter image description here

    Package.configs always like this:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" />
    </packages>
    

    And packagereference usage is like this:

    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    

    I also set this in the property group:

    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    

    enter image description here

    enter image description here

    If you found something issue occurs of intellisense, please just try to build the current project. And then just check whether you can use the package.

    These are the main ways to use dependencies of net framework. If there are any specific errors reported, please share them.