Quickstart: Create and publish a package using Visual Studio (.NET Framework, Windows)

https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio-net-framework

Creating a NuGet package from a .NET Framework Class Library involves creating the DLL in Visual Studio on Windows, then using the nuget.exe command line tool to create and publish the package.

Note

This Quickstart applies to Visual Studio 2017 for Windows only. Visual Studio for Mac does not include the capabilities described here. Use the dotnet CLI tools instead.

Prerequisites

  1. Install any edition of Visual Studio 2017 from visualstudio.com with any .NET-related workload. Visual Studio 2017 automatically includes NuGet capabilities when a .NET workload is installed.

  2. Install the nuget.exe CLI by downloading it from nuget.org, saving that .exe file to a suitable folder, and adding that folder to your PATH environment variable.

  3. Register for a free account on nuget.org if you don't have one already. Creating a new account sends a confirmation email. You must confirm the account before you can upload a package.

Create a class library project

You can use an existing .NET Framework Class Library project for the code you want to package, or create a simple one as follows:

  1. In Visual Studio, choose File > New > Project, select the Visual C# node, select the "Class Library (.NET Framework)" template, name the project AppLogger, and click OK.

  2. Right-click on the resulting project file and select Build to make sure the project was created properly. The DLL is found within the Debug folder (or Release if you build that configuration instead).

Within a real NuGet package, of course, you implement many useful features with which others can build applications. You can also set the target frameworks however you like. For example, see the guides for UWP and Xamarin.

For this walkthrough, however, you won't write any additional code because a class library from the template is sufficient to create a package. Still, if you'd like some functional code for the package, use the following:

using System;

namespace AppLogger
{
    public class Logger
    {
        public void Log(string text)
        {
            Console.WriteLine(text);
        }
    }
}

 Tip

Unless you have a reason to choose otherwise, .NET Standard is the preferred target for NuGet packages, as it provides compatibility with the widest range of consuming projects. See Create and publish a package using Visual Studio (.NET Standard).

Configure project properties for the package

A NuGet package contains a manifest (a .nuspec file), that contains relevant metadata such as the package identifier, version number, description, and more.

Some of these can be drawn from the project properties directly, which avoids having to separately update them in both the project and the manifest.

This section describes where to set the applicable properties.

  1. Select the Project > Properties menu command, then select the Application tab.

2. In the Assembly name field, give your package a unique identifier.

Important

You must give the package an identifier that's unique across nuget.org or whatever host you're using. For this walkthrough we recommend including "Sample" or "Test" in the name as the later publishing step does make the package publicly visible (though it's unlikely anyone will actually use it).

If you attempt to publish a package with a name that already exists, you see an error.

        3. Select the Assembly Information... button, which brings up a dialog box in which you can enter other properties that carry into the manifest (see .nuspec file reference - replacement tokens). The most commonly used fields are Title, Description, Company, Copyright, and Assembly version. These properties ultimately appear with your package on a host like nuget.org, so make sure they're fully descriptive.

4. Optional: to see and edit the properties directly, open the Properties/AssemblyInfo.cs file in the project.

5. When the properties are set, set the project configuration to Release and rebuild the project to generate the updated DLL.

Generate the initial manifest

With a DLL in hand and project properties set, you now use the nuget spec command to generate an initial .nuspec file from the project. This step includes the relevant replacement tokens to draw information from the project file.

You run nuget spec only once to generate the initial manifest. When updating the package, you either change values in your project or edit the manifest directly.

  1. Open a command prompt and navigate to the project folder containing AppLogger.csproj file.

  2. Run the following command: nuget spec AppLogger.csproj. By specifying a project, NuGet creates a manifest that matches the name of the project, in this case AppLogger.nuspec. It also include replacement tokens in the manifest.

  3. Open AppLogger.nuspec in a text editor to examine its contents, which should appear as follows:

猜你喜欢

转载自www.cnblogs.com/chucklu/p/11388203.html