C# Programming
Published in

C# Programming

Service Extension Pattern in .Net Core with Azure Tables

The article demonstrates how to write Service Extensions in .Net Core to configure Dependency Injection with Parameters.

Most of us while configuring services or setting up Dependency Injection in .Net Core, we used methods like UseApplicationContext or similar kinds of methods to configure services in .Net Core Startup. Today we will learn how to build something like that so that our Services, will be easily plugged in .Net Core Host Builder.(maybe Web or Default)

Today we will build a custom library to Easily Connect and Consume Azure Table Storages.

Things Required:

  1. Azure Storage Emulator
  2. Azure Storage Explorer
  3. VS 2019

Nuget Paclage Used:

<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" /><PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" /><PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />


  1. Create a .Net Core Class Library Project in VS 2019
  2. Now we have to think, how we are going to configure our main Services for the host builder.
  3. Install the above-mentioned NuGet packages.
  4. Let’s start, we need to create a class, which will be used to configure our services (options). For our sake, I am extending it from TableClientConfiguration so that we can use those features as well.

5. Now that our AzureTableClientOptions class is prepared, we will create our services. we will create IAzureTableClient interface and it’s implementation AzureTableClient. This will be our main service , that will be consumed by our client projects.

6. Now that our Services has been ready, we will create a Extension Method to add our Service to existing IServiceCollection with required options and service lifetime scope.

7. Now we are almost ready, but before consuming our services, there is one thing we need to do, we will build some extensions method to help us in performing some tasks on CloudTable.

8. Now we are ready to consume our class library project.

9. We will create a .Net Core Web Api project in VS2019. and add our class library project as a project reference.

10. We will make changes in ConfigureServices method in Startup.cs

12. Now we will add our connection string in appsettings.json as below, and you need to run Azure Storage Emulator and open the Azure Storage Explorer to get the connection Strings.

You can extract the connection string from here
"ConnectionStrings": {"AzureTableStorage": "AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=;QueueEndpoint=;TableEndpoint=;"}

13. Now we will create a TestEntity of type TableEntity. This will be used to create the Table.

14. Now we will inject the dependency in Controllers, by default there will be a WeatherForecastController, we will be using the same. In this controller’s get method, we will be creating CloudTable and will perform some tasks.

12. Now we can use helper extension methods of our own built library to perform some operations.

13. Now if you got to your Azure Storage Explorer again, you will be able to find your Table under Tables and also your entities.

Azure Storage Explorer

Now we are done, I have created a NuGet package out of this library, you can find the NuGet package

Start using the NuGet package, it is having the source repository as well enjoy.

Please provide your feedback in the comments section.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arkaprava Sinha

Software Engineer-III @Walmart , Cloud, IoT and DevOps Enthusiast