如何通过NuGet软件包共享源代码以在.NET Core项目中使用

编程入门 行业动态 更新时间:2024-10-25 07:22:59
本文介绍了如何通过NuGet软件包共享源代码以在.NET Core项目中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想提供一小部分源代码(例如帮助器类)以供.NET Core项目(.csproj)使用。 此时,根据不同的博客文章和官方的nuget文档,我以多种不同的方式将源代码与NuGet打包在一起。我使用了nuspec文件来控制源文件在nuget包中的最终位置,例如:

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

我没有包含任何msbuild目标文件或安装脚本。

每当我将NuGet软件包安装到.NET Core项目中(

注意:创建新包时,请不要忘记删除 C:\Users\< UserName> \.nuget\packages 文件夹中的此程序包的nuget缓存,否则,它将始终安装旧程序包。 / p>

使用此方法,将包含源文件

希望这会有所帮助。

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 (docs.microsoft/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.

解决方案

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 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="schemas.microsoft/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:

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.

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

Hope this helps.

更多推荐

如何通过NuGet软件包共享源代码以在.NET Core项目中使用

本文发布于:2023-11-15 12:33:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1594476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:软件包   源代码   项目   NuGet   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!