How to exclude a package from a Class Library NuGet package? - c#

I have a .NET Standard class library project that will be built into a NuGet package. I've installed the docfx.console package so that I can generate a documentation website every time I build.
Now when another project installs my library's NuGet package, the docfx.console NuGet package also gets installed - which I don't want.
In the project properties under Package, I have selected Generate NuGet package on build. This would automatically generate the NuGet package for my library when I build it. But in this tab, I don't see anywhere where I can configure to exclude any packages.
So in order to exclude the docfx.console package, I created a .nuspec file with the following contents:
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<!-- Required elements-->
<id>My.Library</id>
<version>1.0.1</version>
<description>My Library's project dscription.</description>
<authors>Me</authors>
<copyright>My Copyright (c)</copyright>
<!-- Optional elements -->
<dependencies>
<dependency id="docfx.console" version="2.36.1" exclude="build" />
</dependencies>
<!-- ... -->
</metadata>
<!-- Optional 'files' node -->
</package>
But it isn't working. How can I correct it to exclude the docfx.console package when building the NuGet package?
Or, is there any other way I could exclude the docfx.console package from the NuGet package?

How to exclude a package from a Class Library NuGet package?
According to the NuGet official documentation Controlling dependency assets:
You might be using a dependency purely as a development harness and
might not want to expose that to projects that will consume your
package. In this scenario, you can use the PrivateAssets metadata to
control this behavior.
<ItemGroup>
<!-- ... -->
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- ... -->
</ItemGroup>
So, just as UserName commented, you can add <PrivateAssets>all</PrivateAssets> to PackageReference of docfx.console in your project.
To accomplish this, edit your project file .csproj like following:
<ItemGroup>
<PackageReference Include="docfx.console" Version="2.36.2" PrivateAssets="All" />
</ItemGroup>
Or
<ItemGroup>
<PackageReference Include="docfx.console" Version="2.36.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
If you are using .nuspec file, you should move the following dependency info in your .nuspec file:
<dependencies>
<dependency id="docfx.console" version="2.36.1" />
</dependencies>

Related

How to get a list of nuget packages installed via MSBuild macro?

From following question How do I list all installed NuGet packages?
I need to get a list of nuget packages from MSBuild itself.
What I try to do:
<Target Name="test" AfterTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths)"/>
</ItemGroup>
<Message Text="Files #(BuildOutputInPackage -> '%(identity)', ', ')" Importance="high"/>
</Target>
Above code return all references whether its nuget or project references dlls. How exactly to return a list of nuget package that currently project *.csproj used?
Format i.e: packageid:Portable.BouncyCastle version = "1.9.0"
I use old classic non-sdk csproj template and .NET 4.8 and <PackageReference> not packages.config files.
Its very bad to read *.csproj and do regex to get a list of <packageReferences. Maybe there's a recommended MSBuild macro to do this like code shown above <Target....
ReferenceCopyLocalPaths is only "Paths to files that should be copied to the local directory." It is not all resolved references.
Yes, don't regex the project file. There is no need. PackageReference is an ItemGroup.
<Target Name="DisplayPackageReference">
<Message Text="#(PackageReference ->'packageid:%(identity) version = "%(version)"', '%0d%0a')" />
</Target>
But note that PackageReference is what the project is asking for and the version can be floating, e.g Version="3.6.*". PackageReference won't give you the package version that the package reference was resolved to.
If you need to know the specific version used, then you may need to install dotnet, use an exec task to run dotnet list "$(MSBuildProjectFile)" package, and parse the command's output.
Very thankful to Jonathan Dodds for his answer above. I combined some approach with his #(PackageReference) idea to get a version of currently referenced nuget package in old classic csproj template. Because a referenced version will not work with #(PackageReference) only. Also dotnet list not worked well in old classic csproj template (non-sdk).
A code below do following:
(Note this approach is not 100% organic, MSBuild should provide a simple target for that):
Get all references that maybe be nuget or non-nuget (DLLs, etc).
Get only references that included inside #(PackageReference). Jonathan Doods approach above.
Get .nuspec file for that package to know exactly what version current *.csproj referenced.
Create a new nuspec file for current project. with a list of dependencies.
You can then call nuget.exe pack ProjectPath.csproj a ProjectName.nupkg nuget will generated.
Use this code inside Directory.Build.targets or within your .csproj file.
<Target Name="GenerateNuspecFile">
<ItemGroup>
<!-- Get all references -->
<MyReference Include="#(ReferenceCopyLocalPaths)" Condition="%(extension) == '.dll'">
<FirstDir>$([System.IO.Directory]::GetParent(%(RelativeDir)))</FirstDir>
<LibDir>$([System.IO.Path]::GetFileNameWithoutExtension(%(MyReference.FirstDir)))</LibDir>
<SecondDir>$(FirstDir)</SecondDir>
<SecondDir Condition="$(LibDir) != 'lib'">$([System.IO.Directory]::GetParent(%(MyReference.FirstDir)))</SecondDir>
<VersionDir>$([System.IO.Directory]::GetParent(%(MyReference.SecondDir)))</VersionDir>
<PackageDir>$([System.IO.Directory]::GetParent(%(MyReference.VersionDir)))</PackageDir>
<PackageID>$([System.IO.Path]::GetFileName(%(MyReference.PackageDir)))</PackageID>
<NuspecFile>%(MyReference.VersionDir)\%(MyReference.PackageID).nuspec</NuspecFile>
</MyReference>
<!-- Get Non nuget references -->
<ExcludedReference Condition="#(MyReference) != ''" Include="#(MyReference -> '%(PackageID)')" Exclude="#(PackageReference -> '%(identity)')"/>
<!-- Get nuget references after exclude non-nuget references -->
<MyPackage Condition="#(MyReference) != ''" Include="#(MyReference -> '%(PackageID)')" Exclude="#(ExcludedReference -> '%(identity)')"/>
<!-- Read Nuspec File -->
<NuspecContent Condition="#(NuspecFile) != ''" Include="%(MyPackage.NuspecFile)">
<Content>$([System.IO.File]::ReadAllText(%(fullpath)))</Content>
<PackageIDRegex><id>(.*?)</id></PackageIDRegex>
<PackageID>$([System.Text.RegularExpressions.Regex]::Match('%(NuspecContent.Content)', '%(NuspecContent.PackageIDRegex)' ))</PackageID>
<PackageID>$([System.String]::Copy('%(NuspecContent.PackageID)').Replace('<id>','').Replace('</id>', '').Trim())</PackageID>
<VersionRegex><version>(.*?)</version></VersionRegex>
<Version>$([System.Text.RegularExpressions.Regex]::Match('%(NuspecContent.Content)', '%(NuspecContent.VersionRegex)' ))</Version>
<Version>$([System.String]::Copy('%(NuspecContent.Version)').Replace('<version>','').Replace('</version>', '').Trim())</Version>
</NuspecContent>
</ItemGroup>
<!-- Generate Nuspec File with dependencies-->
<PropertyGroup>
<NewLine>%0d%0a</NewLine>
<Space2>%20%20</Space2>
<Space4>$(Space2)$(Space2)</Space4>
<Space6>$(Space4)$(Space2)</Space6>
<HasDependencies>false</HasDependencies>
<HasDependencies Condition="#(MyPackage) != ''">true</HasDependencies>
<Nuspec>$(Nuspec)<?xml version="1.0" encoding="utf-8"?>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)<package >$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space2)<metadata>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<id>$id$</id>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<version>$version$</version>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<title>$title$</title>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<authors>$author$</authors>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<owners>$author$</owners>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<requireLicenseAcceptance>false</requireLicenseAcceptance>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<licenseUrl>https://Your Company.com</licenseUrl>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<icon>PackageIcon.png</icon>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<projectUrl>http://Your Company.com</projectUrl>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<description>$description$</description>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<releaseNotes>$description$</releaseNotes>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<copyright>$copyright$</copyright>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<tags>$title$ Your Company Your Company.com</tags>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<dependencies>$(NewLine)</Nuspec>
<Nuspec Condition="$(HasDependencies)">$(Nuspec)$(Space6)<group targetFramework=".NETFramework4.8">$(NewLine)</Nuspec>
<Nuspec Condition="$(HasDependencies)">$(Nuspec)#(NuspecContent -> '$(Space6)<dependency id="%(PackageID)" version="%(Version)"/>', '$(NewLine)')$(NewLine)</Nuspec>
<Nuspec Condition="$(HasDependencies)">$(Nuspec)$(Space6)</group>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)</dependencies>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space2)</metadata>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space2)<files>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space4)<file src="..\**\PackageIcon.png"/>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)$(Space2)</files>$(NewLine)</Nuspec>
<Nuspec>$(Nuspec)</package>$(NewLine)</Nuspec>
</PropertyGroup>
<WriteLinesToFile Lines="$(Nuspec)" File="$(MSBuildProjectDirectory)\$(MSBuildProjectName).nuspec" Overwrite="true"/>
</Target>
The generated .nuspec file will be:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<licenseUrl>https://company.com</licenseUrl>
<icon>PackageIcon.png</icon>
<projectUrl>http://company.com</projectUrl>
<description>$description$</description>
<releaseNotes>$description$</releaseNotes>
<copyright>$copyright$</copyright>
<tags>$title$ company company.com</tags>
<dependencies>
<dependency id="Company.Globals" version="2022.7.19"/>
<dependency id="Portable.BouncyCastle" version="1.9.0"/>
</dependencies>
</metadata>
<files>
<file src="..\**\PackageIcon.png"/>
</files>
</package>
Its really to hard to achieve anything with MSBuild. Maybe you can create a console app for that and just call console app directly with <Exec Command =""/>. But current approach is friendly with MSBuild. Its not 100% organic approach. MSBuild should provide us a target to get a referenced nuget packages with its referenced version.

Create nuget package for multiple SDK projects

I have 3 .net standard SDK projects(Utils, Reporting and Testing) that I want to pack them into one nuget package called Shared and upload it in my local package management system to be able to be used in my other projects. I was thinking to use the csproj file(s) to fill all the package information so I don't have to use nuspec file(s). One way I tried is to create a new .net standard project called Shared that references all the first 3 projects and only fill the Shared.csproj.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Copyright>Copyright (c) Me</Copyright>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<SkipCopyBuildProduct>true</SkipCopyBuildProduct>
<GenerateDependencyFile>False</GenerateDependencyFile>
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Utils.csproj" />
<ProjectReference Include="..\Reporting.csproj" />
<ProjectReference Include="..\Testing.csproj" />
</ItemGroup>
</Project>
I've tried this msbuild command:
dotnet msbuild -t:pack=Shared.csproj;-p:Configuration=Release;PackageOutputPath=.\release;
and this nuget cli command:
nuget pack Shared.csproj -Build -IncludeReferencedProjects -Prop Configuration=Release
but they don't work. I'm not sure which one is more suited.
Are there more suited alternatives?
Later edit: I've added my code to github with #mu88's suggestion to not create a project only for grouping my other projects but I don't get the 3 projects in my nupkg's libs folder. Do I also specify each project in the dependencies.group section?
I'd personally recommend using a .nuspec file - using a project only as a container to ship multiple assemblies together without any behavior at all doesn't seem right to me.
Such a .nuspec would look like this:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>My.Package</id>
<version>1.0.0</version>
<description>My package</description>
<authors>Me</authors>
<dependencies>
<group targetFramework="netstandard2.0" />
</dependencies>
</metadata>
<files>
<file src="*\bin\Debug\netstandard2.0\*.dll" target="lib\netstandard2.0" />
</files>
</package>
And you have to call nuget pack .\.nuspec (using PowerShell).
The newer approach would be to not use .nuspec at all, but ship the three projects as three independent NuGet packages.

How to inject a custom dependency in an MSBuild NuGet pack generated .nuspec

I'm attempting to migrate to using MSBuild Pack support for using .csproj to generate a projects NuGet package where during development local .dll's are used to build the project but they need to be replaced/swapped to reference an external NuGet package in the generated .nuspec when Using MSBuild to "pack" the project.
The closest documented example of this use-case I could find is Replacing one library from a restore graph
where it suggests you can replace an external NuGet reference:
<PackageReference Include="Newtonsoft.Json" Version="9.0.1">
<ExcludeAssets>All</ExcludeAssets>
</PackageReference>
Which overrides the package to reference to a local .dll instead:
<Reference Include="Newtonsoft.Json.dll" />
I'm trying to do something similar where the project should build against local .dll's that were injected as an artifact from a dependent TeamCity/CI build:
<Reference Include="..\..\lib\net45\ServiceStack.Interfaces.dll" />
<Reference Include="..\..\lib\net45\ServiceStack.Text.dll" />
<Reference Include="..\..\lib\net45\ServiceStack.Common.dll" />
But when using ExcludeAssets=All as per the documentation:
<PackageReference Include="ServiceStack.Common" Version="5.0.0">
<ExcludeAssets>All</ExcludeAssets>
</PackageReference>
The PackageReference doesn't get exported in the generated dependency list, e.g:
<group targetFramework=".NETFramework4.5" />
The closest I've come to getting the preferred behavior is to use ExcludeAssets="compile" so my local project doesn't build against it, except this behavior also gets exported in the .nuspec:
<group targetFramework=".NETFramework4.5">
<dependency id="ServiceStack.Common" version="5.4.0" exclude="Compile,Build,Analyzers" />
</group>
I only want to avoid building against it locally but have it exported as a normal dependency. The other issue with this approach is that I need it to reference a package that hasn't been published to NuGet yet (as all packages are published in lock-step), e.g:
<!-- v5.5.0 is the new version to publish -->
<PackageReference Include="ServiceStack.Common" Version="5.5.0" ExcludeAssets="compile"/>
The build fails that it can't find the unpublished package:
[NU1102] Unable to find package ServiceStack.Common with version (>= 5.5.0)
Effectively I need someway to "inject" the exact dependencies and version I need in the generated .nuspec so it's included in the generated .nuspec dependency list, e.g:
<group targetFramework="net45">
<dependency id="ServiceStack.Common" version="5.5.0" />
</group>
<group targetFramework=".netstandard2.0">
<dependency id="ServiceStack.Common" version="5.5.0" />
</group>
How can we inject a custom dependency in MSBuild generated .nuspec?
Is there some way I can manually declare <dependency/> as above so it's only used when MSBuild/NuGet packs the project? Otherwise is there a way to apply some XML transform to the generated .nuspec so I can manipulate the XML in the .nuspec before it's packed/compressed?
Basically I'm only interested in injecting dependencies when the "pack" target is run so it's ignored/inert in all other targets.
You've probably found another solution for this by now, but I "succeeded" in doing this, by adding some custom targets to the csproj. The solution did leave me with the feeling that maybe I shouldn't have done it.
The steps were
Inject a step before the package generation target, that disables the nupkg generation
Inject a step after the package generation target that modifies the generated nuspec file, and call the package generation target again.
I've modified my fix below so it injects the dependencies mentioned in the post. I've disabled validation here (NoPackageAnalysis=true). You can put it in a .target file and import it from the csproj.
<Project>
<!-- Disable nupkg generation before running pack -->
<Target Name="__DisablePacking" BeforeTargets="GenerateNuspec" Condition="$(NuspecFile) == ''">
<PropertyGroup>
<ContinuePackingAfterGeneratingNuspec>false</ContinuePackingAfterGeneratingNuspec>
</PropertyGroup>
</Target>
<!-- Modify the generated nuspec file and rerun the pack target -->
<Target Name="__EnablePackingAndInjectDependencies" AfterTargets="Pack" Condition="$(NuspecFile) == ''">
<!-- Get the nuspec file name -->
<PropertyGroup>
<_NugetPackOutputAsProperty>#(NuGetPackOutput)</_NugetPackOutputAsProperty>
</PropertyGroup>
<ItemGroup>
<_NugetPackOutputAsItem Remove="#(_NugetPackOutputAsItem)"/>
<_NugetPackOutputAsItem Include="$(_NugetPackOutputAsProperty.Split(';'))" />
</ItemGroup>
<PropertyGroup>
<__NuspecFileName>%(_NugetPackOutputAsItem.Identity)</__NuspecFileName>
</PropertyGroup>
<!-- Create an updated dependencies, with the net46 dependencies copied to a native group -->
<PropertyGroup>
<__NuSpecUpdatedDependencies>
<group targetFramework="net45">
<dependency id="ServiceStack.Common" version="5.5.0" />
</group>
<group targetFramework=".netstandard2.0">
<dependency id="ServiceStack.Common" version="5.5.0" />
</group>
</__NuSpecUpdatedDependencies>
</PropertyGroup>
<!-- Poke them back in -->
<XmlPoke XmlInputPath="$(__NuspecFileName)"
Value="$(__NuSpecUpdatedDependencies)"
Query="/n:package/n:metadata/n:dependencies"
Namespaces="<Namespace Prefix='n' Uri='http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd' />">
</XmlPoke>
<!-- call the pack operation again -->
<PropertyGroup>
<ContinuePackingAfterGeneratingNuspec>true</ContinuePackingAfterGeneratingNuspec>
</PropertyGroup>
<Msbuild
Projects="$(MSBuildProjectFullPath)"
Targets="Pack"
Properties="NuspecFile=$(__NuspecFileName);NoPackageAnalysis=true">
</Msbuild>
</Target>
</Project>
(I was looking to inject a native0.0 framework dependency so my packages could be consumed by c++/cli projects, in case someone should know of an easier way to do it).
Interesting question, and when I looked around for answers
I found Another discussion in Stackoverflow
Where in one of the comments - there was an indication that the pack command only works with framework projects with 4.6 and above - not 4.5.
The .Net Standard 2.0 has to match 4.6.1 based on this blog entry from Microsoft

Empty Nuget package for just dependencies

I have a project I'd like to put all my dependencies into one nuget package. The idea is that when I need to I can just pull in that one nuget package instead of 10 that I require. Is this possible to do?
When I created a library I removed the class file and just pulled in the dependencies but could not get a package to create.
You can do this by setting the IncludeBuildOutput property to false while creating the package.
For example [using VS 2017] -
File > New Project > .NET Core class library
Right click on project and edit csproj to have the following content -
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<PackageId>MetaPackage</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NuGet.Versioning" Version="4.7.0-rtm.5104" />
<PackageReference Include="NUnit" Version="3.10.1" />
</ItemGroup>
</Project>
Right click and build project
Look at the package at <project_dir>\bin\Debug\MetaPackage.1.0.0.nupkg
This will create a package that has no \lib and the nuspec file should have package reference dependencies.
You can read more about IncludeBuildOutput here.

How to include a library in .NET Core 2.0

I don't know much about .NET yet, so I guess I'm missing something obvious.
I created a library (targeted as a DLL file, set for .NET standard 2.0), packaged it both as a DLL file and as a NuGet package. Now I want to use the library in another project, on ASP.NET Core 2.0. How should I do it?
I am currently on a Linux VM, so I use Visual Studio Code, and therefore I would prefer some solution without using the full Visual Studio. I tried some solutions using the full Visual Studio, but that didn't work for me, because I haven't found a reference explorer anywhere.
You would have to reference your library in the .csproj file:
An empty .csproj file would look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
</Project>
Now, you can have two types of references:
Project Reference - You have a project that serves as a class library in your solution and you want to reference it directly:
<ProjectReference Include="..\..\src\mylib.csproj" />
Package Reference - You have a link to a NuGet package:
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.2" />
Inside your .csproj file, the references should be inside an "ItemGroup" block, and each reference type should have its own "ItemGroup".
Here's an example of a .csproj file with some package references and some project references:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\mylib.csproj" />
<ProjectReference Include="..\..\src\mylib2.csproj" />
</ItemGroup>
</Project>
A lot of people recommend one of two solutions:
Copy the library into your solution folder.
cp -r foo/foo ./foo
dotnet sln add foo/foo.csproj
cd bar
dotnet add reference ../foo/foo.csproj
This is a terrible solution.
Don't do this (i.e., copy and paste your library code every time you want to use it. It is bad for obvious reasons).
Setup a local NuGet repository, copy your library into the local repository, and then add it.
nuget add -name "Local" -source /home/doug/packages
nuget add ~/foo/foo.nupkg -source /home/doug/packages
Then install the package:
cd bar
dotnet add package foo
This is an acceptable solution, but the workflow is quite irritating if you are actively working on your library (foo), because the -source path must be absolute.
--
I recommend you look at dotnet add package with local package file, which explains how you can have a local cache of any custom .nupkg files you want to work with.
Basically, just drop this into your solution folder:
File NuGet.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="./packages" />
</packageSources>
</configuration>
(Notice that ./packages is a relative path, that will work even when you check your project out on an entirely different machine or OS.)
Now if you call dotnet add package X it will also look for any file called x.nupkg in your ./packages/ folder.
Now if you want to use any custom local library, all you need to do is:
cp ~/foo/foo.nupkg ./packages
cd bar
dotnet add package foo
(Note: by default NuGet caches your .nupkg files in ~/.nuget and will restore packages from that folder if you call dotnet add package X, even if you have a different X.nupkg in your local ./packages folder. You may find the command dotnet nuget locals all --clear useful if you encounter strange behaviour to ensure you're getting the exact version of the .nupkg file you want, not some arbitrary cached version)
Another way to reference the local package in the .csproj file:
<ItemGroup>
<Reference Include="MyAssembly">
<HintPath>path\to\MyAssembly.dll</HintPath>
</Reference>
</ItemGroup>
Given that the DLL file you want to reference in the new ASP.NET Core 2.0 project is relatively fresh, I suspect you will need to make changes to this original DLL file as you develop the ASP.NET project.
In this situation I would add the original DLL project as part of the ASP.NET solution so you can work on both sets of source code, including setting of breakpoints within the same solution workspace.
NuGet packaging of the original DLL project can be delayed until the first release of your whole combined solution has stabilised and you want to make that DLL file available to a larger developer audience beyond the scope of your ASP.NET application.
A good solution will be to add the library (.dll file) that you want to use to the Project's References of your project in which you want to use the library:
Right Click on the project → Add → Reference → Project → Browse → Path_to_your_generated_library (.dll)
This will automatically generate the following node in the .csproj file:
<ItemGroup>
<Reference Include="DotNetCoreClassLibraryCodeParser">
<HintPath>..\..\DotNetCoreClassLibrary\DotNetCoreClassLibrary\bin\Debug\netcoreapp2.1\DotNetCoreClassLibrary.dll</HintPath>
</Reference>
</ItemGroup>

Categories