Introduction
Nowadays, technology is growing faster and better. It often follows an agile methodology for continuous development and innovation with new versions of programming languages and frameworks. Every month or quarterly we get a new release of products and future roadmaps. It's a good sign of a growing open source community; through sharing knowledge these communities may faster solve problems and improve the quality of products.
In this blog, I’m going to share how to create NuGet packages for common functionality which is being shared & used across all type of frameworks in .NET net programming
Before we start, let’s set up the pre-requisites.
Step 1: Create an account in Nuget.org (https://www.nuget.org/)
Once you create an account, navigate to the profile and create API Keys. An API key is a token that can identify you to NuGet Gallery
Here's a reference link for all command lne cli to manager your NuGet packages: https://docs.microsoft.com/en-us/nuget/reference/nuget-exe-cli-reference 

API Key will look like oy0jbfbh9tmdlx3huhhalmwautb6dynebme4afcyaviz0iBefore
Step 2: Let’s create a small .Net Core class library POC with some common validation functions:- namespace ValidationUtility
- {
- /// <summary>
- /// Rajendra Taradale
- /// </summary>
- public static class Validation
- {
- /// <summary>
- /// RT:It will validate Visa Card Number
- /// </summary>
- /// <param name="ccno"></param>
- /// <returns></returns>
- public static bool CustomVisaCCValidate(string ccno)
- {
- return Regex.Match(ccno, @"^4[0-9]{12}(?:[0-9]{3})$").Success;
- }
- /// <summary>
- /// RT:It will validate MasterCard CC Numder
- /// </summary>
- /// <param name="ccno"></param>
- /// <returns></returns>
- public static bool CustomMasterCardCCValidate(string ccno)
- {
- return Regex.Match(ccno, @"^5[0-9]{12}(?:[0-9]{3})$").Success;
- }
- /// <summary>
- /// RT:It will validate 10 digit phone number
- /// </summary>
- /// <param name="phonenumber"></param>
- /// <returns></returns>
- public static bool CustomPhoneValidate(string phonenumber)
- {
- return Regex.Match(phonenumber, @"^[0-9]{10}$").Success;
- }
- /// <summary>
- /// RT:It will remove the characters from string
- /// </summary>
- /// <param name="phone"></param>
- /// <returns></returns>
- public static string RemoveNonNumeric(this string phone)
- {
- return Regex.Replace(phone, @"[^0-9]+", "");
- }
- /// <summary>
- /// RT:It will validate Date format
- /// </summary>
- /// <param name="date"></param>
- /// <returns></returns>
- public static bool CustomDateValidate(string date)
- {
- return DateTime.TryParse(date, out DateTime dt);
- }
- }
- }

Once the changes are done, right-click on the project and click on the pack to create NuGet packages
Now that we have a NuGet package ready, let's deploy it to nuget.org

- dotnet nuget push CSharpValidationUtility.1.0.0.nupkg -k <API KEY> -s https://api.nuget.org/v3/index.json
Now let's check this package in NuGet portal

Ok, our package is ready and listed after indexing the NuGet packages list. Let's check and test in the client tester console application
Let's install this package and test some methods
Let's call some methods and test using .Net Core Console application:

Let's call some methods and test using .Net Framework (MVC 4.5) Console application:
Conclusion: We can create common functionalities and domain-specific logic under certain NuGet packages. Furthermore, we can deploy them as public and private NuGet repositories and use them across any frameworks. We may also create reusable libraries.

Sunil GuptaPosted Dec 2, 2019, 1:28 AM
Very informative. Thanks for article. Can we follow same approach when we create nuget packages for organization level...
Sourav Kumar DasPosted Dec 2, 2019, 1:17 AM
Nice informative article.