简体   繁体   English

生成的nuget包中缺少Project .dll本身

[英]Project .dll itself is missing in generated nuget package

I am having c# .net core project. 我正在使用c#.net核心项目。 Project name is Master and it has dependencies of external NuGet package as "SpecFlow.CustomPlugin". 项目名称为Master ,它具有外部NuGet程序包的依赖关系,如“ SpecFlow.CustomPlugin”。 Master project has Two classes as Shape.cs and Factory.cs . 主项目有两个类Shape.csFactory.cs

When I generate the NuGet package of Master project so that the implementation of Shape.cs and Factory.cs class can be useful as library into other projects. 当我生成Master项目的NuGet包时,Shape.cs和Factory.cs类的实现可以用作其他项目中的库。 After generating the NuGet package of Master project I only see the "SpecFlow.CustomPlugin" content with it's own dependent dll files but I am not able to see dll of Master project in newly generated NuGet package. 在生成Master项目的NuGet包后,我只看到“SpecFlow.CustomPlugin”内容及其自己的相关dll文件,但我无法在新生成的NuGet包中看到dll的Master项目。

Newly generated NuGet package is included in another project let's say Consumer. 假设消费者是另一个项目中包含的新生成的NuGet包。 Consumer want to use the Shape.cs method and Factory.cs method but it's not accessible. 消费者希望使用Shape.cs方法和Factory.cs方法,但无法访问。

Master project Dependencies looks like: 主项目依赖关系看起来像: 在此输入图像描述

First, make sure that Shape and Factory are public. 首先,确保ShapeFactory是公开的。 If you don't specify their visibility, they're private by default, which means that they can not be used directly by other projects, either as package reference or project references. 如果您不指定它们的可见性,则默认情况下它们是私有的,这意味着它们不能被其他项目直接用作包参考或项目参考。

When you pack master into a NuGet package, the default file it will create is bin\\Debug\\master.1.0.0.nupkg . master打包到NuGet软件包中时,它将创建的默认文件为bin\\Debug\\master.1.0.0.nupkg Assuming master is targetting .NET Standard 2.0, master's dll is saved as lib\\netstandard20\\master.dll in the nupkg . 假设master以.NET Standard 2.0为目标, lib\\netstandard20\\master.dll master的dll保存为nupkg中的lib\\netstandard20\\master.dll You need add/push this nupkg to a nuget feed (can be a directory on your computer or network, nuget.org or you can host your own feed ). 您需要将此nupkg添加/推送到nuget feed(可以是您的计算机或网络上的目录,nuget.org,或者您可以托管自己的Feed )。 Your comsumer project will need a nuget.config that adds the correct NuGet feed as a source, then you add a package reference to the master package. comsumer项目将需要一个nuget.config ,增加了正确的NuGet饲料的来源,那么你添加到主包装基准。 After you restore (which Visual Studio does automatically if you added the package with the UI), then you can use any of master's public classes. 还原后(如果通过UI添加了包,Visual Studio会自动执行该操作),然后可以使用master的任何公共类。

Here's commands you can run on the command line to set up two projects, one is a package, the other will consume it. 您可以在命令行上运行以下命令来设置两个项目,一个是程序包,另一个将使用它。 If you're using .NET Core in Visual Studio, it must have already downloaded the .NET Core SDK, which puts the dotnet cli on your path. 如果您在Visual Studio中使用.NET Core,则它必须已经下载了.NET Core SDK,这会将dotnet cli放在您的路径上。 You'll also need to download nuget.exe from https://www.nuget.org/downloads/ . 您还需要从https://www.nuget.org/downloads/下载nuget.exe。

dotnet new nugetconfig
nuget source -configfile nuget.config add -name local -source feed

# create an isolated nuget environment, because I don't like to populate
# my global packages folder with test packages
nuget config -configfile nuget.config -set globalPackagesFolder=gpf

# create a library, pack it, and add the nupkg to our local feed
dotnet new classlib -n MyLib
dotnet pack MyLib\MyLib.csproj -o nupkgs\
nuget add MyLib\bin\Debug\MyLib.1.0.0.nupkg -s local

# create console app and reference MyLib
dotnet new console -n MyApp
dotnet add MyApp\MyApp.csproj package MyLib --version 1.0.0

#if you want to open these projects in Visual Studio
dotnet new sln -n sample
dotnet sln add MyLib\MyLib.csproj
dotnet sln add MyApp\MyApp.csproj
start sample.sln

Normally you wouldn't use a NuGet package to use one project from another project in the same solution. 通常,您不会使用NuGet包在同一解决方案中使用另一个项目中的一个项目。 Just make them project references, and when you pack a project into a NuGet package, the project reference becomes a NuGet dependency. 只需为它们提供项目引用,然后将项目打包到NuGet包中时,该项目引用将成为NuGet依赖项。 I only did it this way to demonstrate how to easily consume a package that you created yourself. 我这样做只是为了演示如何轻松使用自己创建的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM