Stu*_*ang 9 .net c# nuget .net-standard
我正在尝试创建一个 .NET 标准 nuget 内容文件包(没有托管程序集),供 .NET Framework 程序集使用。我的目标是将此文件文件夹复制到消费程序集的 bin 文件夹中。
这个问题类似于Add native files from NuGet package to project output directory(并使用这个 nuget 包http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/),但我需要我的 nuget 包.NET 标准
我已经在 contentfiles\any\any\myfiles 下的程序集中添加了文件,并将以下内容添加到 nuspec 文件的元数据部分:
<contentFiles>
<files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>
Run Code Online (Sandbox Code Playgroud)
当我尝试将其添加到 .NET 框架程序集时,出现错误
无法安装包“myPackage 1.0.0”。您正在尝试将此包安装到以“.NETFramework,Version=v4.6.2”为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。有关更多信息,请联系软件包作者。
我还尝试将文件添加到项目的根目录,并在 nuspec 文件的元数据元素下方添加以下内容:
<files>
<file src="myfiles\**\*" target="myfiles" />
</files>
Run Code Online (Sandbox Code Playgroud)
完整的 nuspec 文件:
<?xml version="1.0"?>
<package>
<metadata>
<id>myPackage</id>
<version>1.0.0</version>
<title>myPackage</title>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>my files/description>
<releaseNotes></releaseNotes>
<tags></tags>
<contentFiles>
<files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="myfiles\**\*" target="myfiles" />
</files>
</package>
Run Code Online (Sandbox Code Playgroud)
此外,在 csproj 中,我还将 TargetFramework 元素更改为:
<TargetFrameworks>netstandard1.3;netstandard2.0;net40;net45;net46</TargetFrameworks>
Run Code Online (Sandbox Code Playgroud)
解决方案:
最后,这是解决方案:“myFiles”是一个位于项目根目录中的文件夹,包含许多不同的文件和子目录。
nuspec 文件:
<?xml version="1.0"?>
<package >
<metadata>
<id>myPackage</id>
<version>1.0.0</version>
<title>My Package</title>
<tags></tags>
<dependencies>
<group targetFramework=".NETStandard2.0" />
<group targetFramework=".NETFramework4.6.2" />
</dependencies>
<contentFiles>
<!-- this sets "CopyIfNewer" on all files in the project that references this package -->
<files include="**/*" buildAction="None" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="myFiles\**\*" target="contentFiles\any\any\myFiles"/>
</files>
</package>
Run Code Online (Sandbox Code Playgroud)
引用 nuget 包的项目似乎有一个名为“myfiles”的文件夹(所有文件上都有一个链接图标),它将在构建时复制到 bin 文件夹。
有一些推断的方法可以做到这一点,但是 add 可以在此处添加 targetFramework 作为依赖项设置。
<dependencies>
<group targetFramework=".NETStandard1.3" />
<group targetFramework=".NETStandard2.0" />
<group targetFramework=".NETFramework4.0" />
<group targetFramework=".NETFramework4.5" />
<group targetFramework=".NETFramework4.6" />
<group targetFramework=".NETFramework4.6.2" />
</dependencies>
Run Code Online (Sandbox Code Playgroud)
我还建议将 .dll 文件与 nuspec 文件的文件部分中的内容文件明确分开,并引用约定lib\<targetName>\..(这实际上与我上面提到的推断情绪相关,但我没有太多细节目前提供)
所以你的 nuspec 文件将如下所示:
<?xml version="1.0"?>
<package>
<metadata>
<id>myPackage</id>
<version>1.0.0</version>
<title>myPackage</title>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>my files/description>
<releaseNotes></releaseNotes>
<tags></tags>
<contentFiles>
<files include="any/netstandard1.3/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/netstandard2.0/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net40/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net45/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net46/myfiles/*" buildAction="None" copyToOutput="true" />
<files include="any/net462/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>
<dependencies>
<group targetFramework=".NETStandard1.3" />
<group targetFramework=".NETStandard2.0" />
<group targetFramework=".NETFramework4.0" />
<group targetFramework=".NETFramework4.5" />
<group targetFramework=".NETFramework4.6" />
<group targetFramework=".NETFramework4.6.2" />
</dependencies>
</metadata>
<files>
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\netstandard1.3\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\netstandard2.0\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net40\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net45\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net46\<yourprojectassembly.dll>" />
<file src="<your-path>\<yourprojectassembly.dll>" target="lib\net462\<yourprojectassembly.dll>" />
<file src="<your-path>\myfiles\*" target="content\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\netstandard1.3\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\netstandard2.0\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net40\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net45\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net46\myfiles" />
<file src="<your-path>\myfiles\*" target="contentFiles\any\net462\myfiles" />
</files>
</package>
Run Code Online (Sandbox Code Playgroud)
引用您的 nuget 包的项目的屏幕截图。(我的内容是一个文本文件,目标是"lib\<target>\any"尊敬的)
值得注意的是,尽管引用您的包的程序集中的内容可能存在一些行为注意事项。
在上图中,来自 nuget 包的内容默认引用为“不复制到输出”,C# 编译为“BuildAction”。