Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share source code via NuGet packages for use in .NET Core projects

I want to make small pieces of source code (e.g. helper classes) available for use in .NET Core projects (.csproj). At this point I packaged the source code with NuGet in many different ways according to different blog posts and the official nuget docs. I used a nuspec file to control where my source files will end up in the nuget package, e.g.:

<files>
    <file src="*.cs" target="content/LruCache" />
    <file src="*.cs" target="contentFiles/cs/any/LruCache" />
</files>

I did not include any msbuild targets file or install script.

Whenever I install the NuGet package into a .NET Core project (https://docs.microsoft.com/en-us/dotnet/core/tools/csproj) I simply don't get anything there. No source files will be included into my project. I tried different settings for the <PackageReference/> node in the .csproj (PrivateAssets, etc.) without success.

Is it meant to be possible at all? If so, how should it be done?


Background:
The reason for doing this is some kind of diamond problem where we have projects B and C both using helper class A and a third project D using B and C. In this situation I don't want to deal with assembly version conflicts when different (incompatible) versions of A have been used in B and C.

like image 425
uebe Avatar asked Oct 18 '18 18:10

uebe


People also ask

How do I share a NuGet package?

If you already installed Nuget Package for Root Project You Will See Manage Button. Now, choose which Projects you want to share the Nuget Package and press Ok. According to your selection, it will apply/share the nugets to all the sub-projects . So, it's complete now.

How do I add a source to a NuGet package?

In the Options window, expand the NuGet Package Manager node and select Package Sources. To add a source, select +, edit the Name, enter the URL or path in Source, and then select Update. The source now appears in the Package source dropdown list.


1 Answers

Is it meant to be possible at all? If so, how should it be done?

The answer is yes. Since you test project type is .net core. You should use contentFiles instead of content. content is used for packages.config. Check the Using the contentFiles element for content files and blog NuGet ContentFiles Demystified for more details.

So your .nuspec file should be:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>MyTestCore</id>
    <version>5.0.0</version>
    <authors>TestContentFile</authors>
    <owners>TestContentFile</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <contentFiles>
      <files include="any/any/Test.cs" buildAction="content" flatten="true" copyToOutput="true"/>
    </contentFiles>
  </metadata>

  <files>
    <file src="contentFiles/any/any/Test.cs" target="contentFiles/any/any/LruCache" />
  </files>
</package>

The nuget package should be looks like:

enter image description here

Note: When you create a new package, do not forgot to remove the nuget cache for this package in the C:\Users\<UserName>\.nuget\packages folder, otherwise, it always install the old package.

enter image description here

With this method, the source files will be included into your project.

Hope this helps.

like image 123
Leo Liu-MSFT Avatar answered Sep 27 '22 18:09

Leo Liu-MSFT