Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

c# - Add a package with a local package file in 'dotnet'

Using the dotnet command line tool, how can I add a reference to an existing local package that is not downloaded with NuGet?

I have tried adding a local package to a project bar with dotnet:

dotnet add package /Users/sakra/foo/bin/Debug/foo.1.0.0.nupkg

The package foo.1.0.0.nupkg has been created with dotnet pack in a different project. The command dotnet add package however tries to download the file foo.1.0.0.nupkg from https://api.nuget.org/ which of course fails.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There isn't a way to directly install a single .nupkg package. NuGet can only install and restore from feeds, so you'll need to add the directory where the package is in as a feed.

To do this, add a NuGet.Config file that adds the location of the directory as a feed, so you don't have to add the source parameter to each NuGet-related command (especially dotnet restore):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="local-packages" value="../foo/bin/Debug" />
  </packageSources>
</configuration>

Alternatively in .NET Core 2.0 tools / NuGet 4.3.0, you could also add the source directly to the .csproj file that is supposed to consume the NuGet feed:

<PropertyGroup>
  <RestoreSources>$(RestoreSources);../foo/bin/Debug;https://api.nuget.org/v3/index.json</RestoreSources>
</PropertyGroup>

This will make all commands be able to use the package:

  • dotnet add package foo (optionally add -v 1.0.0)
  • dotnet restore
  • dotnet run

dotnet add package foo will add a package reference (assumption here, version 1.0.0) to *.csproj:

<ItemGroup>
+    <PackageReference Include="foo" Version="1.0.0" />
</ItemGroup>

Note that during development, if you change the NuGet package, but don't increment its version in both the project that produces the .nupkg file and in the project that consumes it, you'll need to clear your local packages cache before restoring again:

dotnet nuget locals all --clear
dotnet restore

I have created a small example project at https://github.com/dasMulli/LocalNupkgExample


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...