Share Code Between ASP.NET Core MicroServices

Microservice architecture or microservices is a method of developing software applications as a suite of independently deployable, small, modular services in which each service runs a unique process and communicates through a well-defined, lightweight mechanism to serve a business goal.

One of the key features of this pattern is to build up small efficient teams that are individually responsible for developing, release, testing and maintenance of a particular microservice. Thus there will always be a problem of duplicate codes across repositories or across teams.

In this post we will be talking about how we can write common codes like common class libraries in ASP.NET based microservices and how to share them across services so that it can be used by all the teams and we can follow the DRY principle.

Prerequisites

  • NET Core 1.1 SDK you can download it from here.

Lets begin by creating a class library in .Net core which can be reused by other projects.

Step 1 - Create a .Net core class library

We will be using the dotnet cli tool to create our class library project. If you have visual studio 2017 installed, you can use the .NET core templates as well.

  • >> dotnet new classlib -n CommonUtil

The above command will create a .NET core class library and name it CommonUtil.

For more options while using the dotnet cli tool please visit here.

Step 2 - Build and package your class library

Now comes the main part of converting our .NET core class library into a nuget package.

Note

For those who are new to Nuget, Nuget is the package manager for the Microsoft development platform including .NET. The NuGet client tools provide the ability to produce and consume packages. The NuGet Gallery is the central package repository used by all package authors and consumers.

To know more about Nuget please visit here.

Now assuming that you are in the root folder where you have created the project earlier i.e CommonUtil, we will run the following commands.

  • >> dotnet restore
  • >> dotnet pack --output nupkgs

This will create a nuget package out of our class library and put the .nupkgfile in the output folder mentioned in the command. Since we have not mentioned the — no-build flag in the dotnet pack command, pack will 1st build the project and then create the .nupkg file.

For more information about the dotnet pack command please visit here.

Step 3 - Versioning the package

The above build step will by default create a package with the file name <project_name>.1.0.0.nupkg, in our case it is CommonUtil.1.0.0.nupkg. So if we see that 1.0.0 i.e the default version of our package has been added to the package name by the dotnet cli tool.

How to specify the package version while creating the package?

We will need to add few configuration to give explicit versioning of our .Net core library package.

Open the .csproj file and then add the below mentioned attributes under PropertyGroup.

  1. <PropertyGroup>  
  2.     <TargetFramework>netstandard1.6</TargetFramework>  
  3.     <VersionPrefix>1.0.0</VersionPrefix>  
  4.     <VersionSuffix>$(VersionSuffix)</VersionSuffix>  
  5. </PropertyGroup>  

In the VersionPrefix you can specify the version of your package and in the VersionSuffix we have specified $(VersionSuffix), so that we can add version suffix in a switch while running the dotnet pack command.

dotnet pack --output nupkgs --version-suffix <VERSION_SUFFIX>

Step 4 - Upload/push and publish package to NuGet.org

Now that we have the nuget package ready, our next step will be to push the package or upload it to the nuget gallery.

For this 1st off all you will either need a Microsoft account or a Nuget.org account to login.

In this demo I have used my Microsoft account to login to Nuget. You can create your own Nuget.org account form here.

Once you have logged into Nuget.org now we need to navigate to upload package tab.


upoad package

We now need to select the .nupkg file that we created right now by the dotnet pack command and upload the file. Once the file is uploaded you will get a few fields to modify before submitting the details.


verify details

After editing all the details once you have verified all the details you can publish your package. It will appear in the search list if the appear in listed in search result dropdown is enabled.

In the above demo we have shown how to upload and publish the package through the portal. There are other ways to do that as well i.e by using the dotnet cli tool itself.

dotnet-nuget push - Pushes a package to the server and publishes it.

To know more about this command please see here.

Step 5 - Reference your package in other projects

Now that we have published the package to Nuget.org, we should be able to reference the same package from other projects.

Open the .csproj file of the project where you want to add the dependency, and then add reference to the nuget package that we have uploaded in the previous step inside the <ItemGroup> tag and inside <PackageReference> as shown below:

  1. <ItemGroup>  
  2.     <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />  
  3.     <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />  
  4.     <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />  
  5.     <PackageReference Include="<Your package name>" Version="<your package version>" />   
  6. </ItemGroup>  

Conclusion: So in this post we have seen how to package and publish .NET core class library and how to reference those packages from other projects. If you think this information is helpful do recommend this post. Until then happy exploring :)

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now