Tuesday, May 22, 2018

Create A NuGet Package Using Visual Studio 2017

Hello all!

You may have noticed that the trend in adding components or dll's to a project is through a technology called NuGet Package and with that I'll demonstrate on how to create a simple C# Library NuGet package using Visual Studio 2017.
Below are the steps to follow.
1. First is to create a C# class library project called StringLib and add a class with a single method to convert the first letter of a string to uppercase.
public class ToUpperCase
    {
        /// <summary>
        /// Change first letter to uppercase
        /// </summary>
        public string ToUpperFirstLetter(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }

            char[] a = s.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }
    }

2. Build the project to generate a .dll file.
3. Copy the .dll or any files to be included in the package to a shared drive.
4. Download nuget.exe file from this website: Nuget.org
5. Copy the nuget.exe file to the root of your project.
6. Open command prompt as an administrator, change the directory to the root of your project and execute statement "nuget spec" to create a Package.nuspec file. 7. Edit the information of the nuspec file based on the project's id, description, authors/owners, version, purpose and location of the files. Make sure that the location of the assembly and file(s) included are correct. 8. Navigate again to the command prompt and execute statement "nuget pack Package.nuspec". This will create a a nuget package file at the root of the project such as "StringLib.1.0.0.nupkg". 9. Copy the newly created nuget package to the shared drive.
Now that we have successfully created the package, we need to add it in Visual Studio's NuGet Package Manager and use it to one of our projects.
1. Open Visual Studio, and go to Tools->Options->NuGet Package Manager-> Package Sources
2. Add the newly created package by adding an appropriate Name and it's location(shared drive). Click Ok. To use the package in a .NET Project:
1. Right click on the project and select "Manage NuGet packages".
2. Click Browse and then select the package from the "Package source:" dropdown.
3. Click Install.

That's it. You should be able to use the string method you created earlier. :-)

0 comments:

Post a Comment