How to share source code via NuGet packages for use in .NET Core projects(如何通过 NuGet 包共享源代码以用于 .NET Core 项目)
问题描述
我想让小段源代码(例如帮助程序类)可用于 .NET Core 项目 (.csproj).至此,我根据不同的博文和官方的 nuget 文档,以多种不同的方式用 NuGet 打包了源代码.我使用了一个 nuspec 文件来控制我的源文件在 nuget 包中的最终位置,例如:
<文件><file src="*.cs" target="content/LruCache"/><file src="*.cs" target="contentFiles/cs/any/LruCache"/></文件>
我没有包含任何 msbuild 目标文件或安装脚本.
每当我将 NuGet 包安装到 .NET Core 项目中时 (
注意: 创建新包时,不要忘记在 C:Users
使用此方法,源文件将包含到您的项目中.
希望这会有所帮助.
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.
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:
Note: When you create a new package, do not forgot to remove the nuget cache for this package in the C:Users<UserName>.nugetpackages
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 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过 NuGet 包共享源代码以用于 .NET Core 项目


基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01