Is there a way to publish a .NET Standard Library without producing a .nupkg in Visual Studio 2019?

元气小坏坏 提交于 2019-12-24 20:05:32

问题


My understanding is that when you publish a .NET Standard Library, the expected behavior is to produce a .nupkg.

I have internal .Net Standard libraries that I maintain for use on in-house projects. A typical workflow would be to publish the libraries and then copy the published files to consuming projects. Is this not the way I should be doing things? I'm a bit confused by the lack of options when publishing a .NET Standard Library -- being that there is a .nupkg without a choice to publish in a different form.

What I want to get from publish is just a collection of necessary library files in the publish directory (not inside a nupkg). Is there a way to do this?


回答1:


My understanding is that when you publish a .Net Standard Library, the expected behavior is to produce a .nupkg. What I want to get from publish is just a collection of necessary library files in the publish directory (not inside a nupkg). Is there a way to do this?

For normal .net framework projects, if you reference one nuget package and build it. You then get a output folder(bin\debug or bin\release) with the Project.dll and Package.dll in it. And that's what you want.

But for .net standard project, it will only output the Project.dll by default. Also it's intended behavior, see comment1, comment2. And that's why I suggest you need to manually edit the xx.csproj to get your expected behavior during build. That's by design that it won't copy the nuget dependencies to output, and this is the workaround that may meet your needs.

As for why the publish output is a xx.nupkg when you right-click project node=>Publish in VS IDE(.net standard project), please check this document:The primary distribution vehicle for the .NET Standard reference assemblies is NuGet packages. I assume that's why the output of Publish is a xx.nupkg, hard to say but, it could be by design...

Edit:

Is there a way to publish a .Net Standard Library without producing a .nupkg in Visual Studio 2019?

If you're referring to the Publish option for .net standard project.(Right click the project name of .net standard project in Solution Explorer=>Publish). I'm afraid the answer is negative. This option is by design and for now no other options like publish a folder with all referenced assemblies are supported. You may need to add a Feature Request in DC.

Hope my clarifications make some help and if I misunderstand anything, feel free to correct me :(




回答2:


A typical workflow would be to publish the libraries and then copy the published files to consuming projects. Is this not the way I should be doing things?

No. That's just about the worst way to do things.

If/as you're not using locally generated Nuget packages you should be using project references:

When you have a project that produces an assembly, you should reference the project and not use a file reference. The advantage of a project-to-project reference is that it creates a dependency between the projects in the build system.

Every solution which contains a project which references one or more of your in-house libraries should also contain the libraries (and any in-house libraries those libraries reference) in the solution.

I'll run through this creating some throwaway projects as I go, but the techniques apply to existing code too - you'd just be adding them to the solution rather than creating new ones:

  • Create ClassLibrary1 targeting .NET Standard 2.0
  • Add a new project to the solution: ClassLibrary2 targeting .NET Standard 2.0
  • Let's install a popular Nuget package to ClassLibrary2: Newtonsoft.Json
  • Right click on ClassLibrary1 in the solution explorer, click Add and then click Reference. In the project tab tick ClassLibrary2 and click OK. ClassLibrary1 now has a project reference to ClassLibrary2 and the following has been added to ClassLibrary1.csproj:
  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" />
  </ItemGroup>
  • Add a .NET console app project, ConsoleApp1

  • Add a project reference for ClassLibrary to ConsoleApp1

  • Save the solution

  • Build, with a release configuration, the solution

  • Everything we need is in the console app's bin folder, and there is no Nupkg folder in sight:

  • If we create another solution and add our libraries to it, or reuse this solution, and then add a .NET Core console app to it, and have that app take a project reference to ClassLibrary1 and build, again there is no Nupkg anywhere in the folder tree.

  • However, we publish .NET Core apps. I, at least, like to publish my .NET Core apps (for internal use) as 'self-contained'. To do this, I right click on the .NET Core app in the Solution Explorer, click Publish, and choose Folder as the target. I click Create Profile and then Edit and change the deployment mode to self-contained, and then save. I then publish the app (not the libraries) and I get all the files I need in my publish folder, including the in-house libraries and .NET Core (don't worry, the Newtonsoft DLL is there too, it's just not visible in the screenshot):

The only times I get a Nuget package built are:

  • if I explicitly publish the in-house libraries, or

  • if I have <GeneratePackageOnBuild>true</GeneratePackageOnBuild> in the project file [example]

Doing it the "Nuget way" is a great and valid approach if you're distributing the libraries via a private package feed; but since that's not what you want to do - don't publish the libraries! Take project references to them instead, which means adding the libraries to the solutions which consume them



来源:https://stackoverflow.com/questions/58101078/is-there-a-way-to-publish-a-net-standard-library-without-producing-a-nupkg-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!