简体   繁体   中英

Project .dll itself is missing in generated nuget package

I am having c# .net core project. Project name is Master and it has dependencies of external NuGet package as "SpecFlow.CustomPlugin". Master project has Two classes as Shape.cs and Factory.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. 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.

Newly generated NuGet package is included in another project let's say Consumer. Consumer want to use the Shape.cs method and Factory.cs method but it's not accessible.

Master project Dependencies looks like: 在此输入图像描述

First, make sure that Shape and Factory are public. 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 . Assuming master is targetting .NET Standard 2.0, master's dll is saved as lib\\netstandard20\\master.dll in the nupkg . 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 ). 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. 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.

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. You'll also need to download nuget.exe from https://www.nuget.org/downloads/ .

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. Just make them project references, and when you pack a project into a NuGet package, the project reference becomes a NuGet dependency. I only did it this way to demonstrate how to easily consume a package that you created yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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