Create a new class library project using CLI
dotnet new classlib -o MyLibrary
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> </Project>
Now add package details to build as a nuget package on project file
<PropertyGroup>
<TargetFramework>netstandard2.0;</TargetFramework>
<PackageId>SampleLibrary</PackageId>
<Version>0.1</Version>
<Description>My SampleLibrary</Description>
<Copyright>My Company</Copyright>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
</PropertyGroup>
IsPackable & GeneratePackageOnBuild are required to build as a .nupkg file.
If you want to Target Multiple Frameworks
change TargetFramework to TargetFrameworks and add ';' between frameworks
<TargetFrameworks>netstandard2.0;net452;net48;</TargetFrameworks>
Add a project reference
<ItemGroup>
<ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" />
</ItemGroup>
Include framework references
<ItemGroup> <!-- keep references unless they are to package files. See the section about the NuGet upgrade below. --> <Reference Include="System.Configuration" /> </ItemGroup>Include file as a content
<ItemGroup> <Content Include="Js\JavaScript1.js" /> </ItemGroup>Add a nuget package
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
</ItemGroup>
Add pdb file into nupkg
Add the following line into the
<PropertyGroup>
element
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Compile project
At this stage, project with compile and you will see package file in
bin\Debug\SampleLibrary.0.1.0.nupkg folder, when open nupkg with an archive tool like Winzip,7Zip or Winrar,
ClassLibrary2 dll will not be included in the nupkg file.
To Fix this issue
Add the following line into the
<PropertyGroup>
element
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
Add new target between
<project>
tags
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))"/>
</ItemGroup>
</Target>
Include extra files into build
<!-- the defaults -->
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
include there lines into csproj file.
<ItemGroup>
<Content Include="**\*.js" />
</ItemGroup>
Filly working csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<PackageId>SampleLibrary</PackageId>
<Version>0.1</Version>
<Description>My SampleLibrary</Description>
<Copyright>My Company</Copyright>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<Content Include="**\*.js" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget',
'ProjectReference'))" />
</ItemGroup>
</Target>
</Project>