使用 dotnet pack 创建具有依赖关系但没有我自己的代码的 NUGET

Using dotnet pack creates NUGET with dependencies but without my own code

我有一个简单的 .NET Core 3.1 class 库,它在 classes 中实现了一些日志记录功能,并且有一个 nlog.config 文件。 .csproj 看起来像这样

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <IsPackable>true</IsPackable>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <NuspecFile>Sima.Logging.nuspec</NuspecFile>
  </PropertyGroup>
      
  <ItemGroup>
    <PackageReference Include="NLog" Version="4.7.12" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
  </ItemGroup>
</Project>

如您所见,我的 .csproj 引用了一个 .nuspec 文件。我需要这样做,因为我想将 nlog.config 作为内容文件包含在内。 .nuspec 看起来像这样

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>Sima.Logging</id>
    <version>1.0.0</version>
            
    <contentFiles>
      <files include="**/nlog.config" buildAction="Content" copyToOutput="true" flatten="true" />
    </contentFiles>
            
    <dependencies>
      <group>
        <dependency id="NLog" version="4.7.12" exclude="Build,Analyzers" />
        <dependency id="NLog.Web.AspNetCore" version="4.9.3" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
        
  <files>
    <file src="contentFiles\**" target="contentFiles" />
  </files>
</package>

在使用 dotnet pack 打包我的库后,nupkg 仅包含对 NLog 的两个依赖项,但 none 我的实际代码(在我的 class 中)包含在包中.

似乎 NuGet 不知道实际构建什么。我错过了什么?

创建 Nuget 包 (*.nupkg) 时,您必须定义要包含在该包中的所有文件。在 nuspec 配置中,您缺少以下内容:

<files>
   <file src="bin\Debug\*.dll" target="lib" />
</files>

其中 bin\Debug\ 是您构建的输出路径。如需进一步参考,请访问 the documentation.