Implementing a Custom Provider for Cloud Ninja Metering Block

In this post, I will explore how to create a new provider and plug it into to the CNMB framework. One of the requirements for your application could be to count the number of rows on a table per tenant periodically, and save that for future use, say to understand the heaviest users of the data.

To create a custom provider you will need to complete these steps:

  1. Implement a class deriving from MeteringProviderBase that collects data, and publishes to the repository
  2. Implement the configuration class deriving from ProviderElement 
  3. Decorate the provider class with the configuration class’ name
  4. Implement abstract method RegisterType on the configuration class
  5. Add the provider’s configuration to your host’s configuration

For this, the first step is of course to get the copy of CNMB source from Then you will need to build the projects first. You will notice there will be a need to refresh some of the NuGet packages, like Reactive Extensions and Transient Fault Handling block from P&P. We have published the source with NuGet “Enable Package Restore” but there might be a need to manually refresh those.

Implementing the provider class

I will continue by adding a new Class Library project to the solution named “CloundNinja.Metering.RowCountsProvider” and reference the project “CloudNinja.Metering.Providers”. I will then get rid of the default Class1.cs file, and add a new class derives from MeteringProviderBase , named  RowCountsProvider. The new class needs to implement the following abstract members of the base class:

The base class, MeteringProviderBase has a constructor with the following signature that we will need to pass the objects for. These dependencies are used internally by the base class to log anything that happens, post the measurement results to for persisting them, and get the information for the context we need to meter. We will also need to reference the CloudNinja.Metering.Common project for the types on the signature.

Now, we create a constructor for our class, so we can pass those in while registering the provider’s class during the block’s bootstrap time.

We do not really have anything to initialize, or get rid of. So we simply say that the provider is initialized, and stopped using the LogInformation method on the ILogger interface. The major work to be done is at the Collectmethod.

With this provider, what we can do is to have a list of connection strings, one table each, with a table name to query, and the columns on where to get the tenant ID from and which column to count. We can populate this in the configuration, and pass that in at the constructor. Assuming the list of connection strings and related information as above is passed in, then we can query those data sources in parallel, get the results and write to the metering repository.

I am not going to describe the full implementation for the Collect method here. Please feel free to download the source at the link.

Implementing the configuration class

Rest is pretty straightforward. First create a class named TenantRowCountConnectionCollectionElement deriving from ConfigurationElement, and add the properties for ServerAlias, Connect string, table name to collect the data from, the column names where the tenant ID resides and the column to count (we do not want to do a COUNT(*)), and last but not least, a tenant Id pattern, as used throughout the block.

Next step is to create a collection deriving from ConfigurationElementCollection, and name it as TenantRowCountConnectionCollection, to contain the connection elements as defined above. And the last class is one deriving from ProviderElement which also implements the RegisterType method. Let’s name that class as TenantRowCountProviderSettingsElement.

In the RegisterType method, we will register the provider with the Unity container to get loaded by the metering block core.

As the last step as far as coding is concerned, we will need to tell the ProviderElement about which classes to load to read the rest of the configuration. This is going to be done through decorating the provider with the TenantRowCountProviderSettingsElement class’s type name.

Decorating the provider class for the configuration

We will need to tell the infrastructure, as we mentioned above about the configuration type for that particular provider. To do so, we will need to add this attribute to the provider class:

Adding the configuration

The configuration of your host application should contain a provider definition like the following:

You can find the sample project file in the downloads section of

Have a great weekend!

Leave a Reply