Creating a Common NuGet Package for all .NET Frameworks

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:
  1. namespace ValidationUtility  
  2. {  
  3.     /// <summary>  
  4.     /// Rajendra Taradale  
  5.     /// </summary>  
  6.     public static class Validation  
  7.     {  
  8.     
  9.         /// <summary>  
  10.         /// RT:It will validate Visa Card Number  
  11.         /// </summary>  
  12.         /// <param name="ccno"></param>  
  13.         /// <returns></returns>  
  14.         public static bool CustomVisaCCValidate(string ccno)  
  15.         {  
  16.             return Regex.Match(ccno, @"^4[0-9]{12}(?:[0-9]{3})$").Success;  
  17.         }  
  18.   
  19.         /// <summary>  
  20.         /// RT:It will validate MasterCard CC Numder  
  21.         /// </summary>  
  22.         /// <param name="ccno"></param>  
  23.         /// <returns></returns>  
  24.         public static bool CustomMasterCardCCValidate(string ccno)  
  25.         {  
  26.             return Regex.Match(ccno, @"^5[0-9]{12}(?:[0-9]{3})$").Success;  
  27.         }  
  28.   
  29.         /// <summary>  
  30.         /// RT:It will validate 10 digit phone number  
  31.         /// </summary>  
  32.         /// <param name="phonenumber"></param>  
  33.         /// <returns></returns>  
  34.         public static bool CustomPhoneValidate(string phonenumber)  
  35.         {  
  36.             return Regex.Match(phonenumber, @"^[0-9]{10}$").Success;  
  37.         }  
  38.   
  39.         /// <summary>  
  40.         /// RT:It will remove the characters from string  
  41.         /// </summary>  
  42.         /// <param name="phone"></param>  
  43.         /// <returns></returns>  
  44.         public static string RemoveNonNumeric(this string phone)  
  45.         {  
  46.             return Regex.Replace(phone, @"[^0-9]+""");  
  47.         }  
  48.   
  49.         /// <summary>  
  50.         /// RT:It will validate Date format   
  51.         /// </summary>  
  52.         /// <param name="date"></param>  
  53.         /// <returns></returns>  
  54.         public static bool CustomDateValidate(string date)  
  55.         {  
  56.             return DateTime.TryParse(date, out DateTime dt);  
  57.         }  
  58.     }  
  59. }  
 In order to make sure it will work in .Net core and .Net Standard framework, let's add the below configuration in project.
  
 
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 
 
  1. 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.