Custom Extension for Azure Functions — Part 2 — Bindings

This article is part two of the two-part series. In my previous article, we looked into how to create a custom trigger. In this article, we will look into how to create a custom binding.

Since this a continuation of my previous article, I highly recommend reading part one before this one so that you get a basic idea of fundamentals of the custom extension and what are going to build.

For the sake of clarity, I will summarize what we are going to explore. In part one, we understand the basics of custom bindings and how to create a custom trigger. We developed a custom NATS Trigger and a sample application to test the trigger in part one.

In this article, we will look into how to create a custom NATS binding.

To create a custom binding:

  1. Define a class that extends from Attribute.
  2. Create a class that extends the IAsyncCollector interface. This interface defines methods to AddAsync and FlushAsync. The system will call the AddAsync function to send data to external resources.
  3. Create a class that implements the IConverter interface. This interface has one method:
    Convert:- The system calls this method to create the AsyncCollector class.
  4. Create a class that implements the interface IExtensionConfigProvider. Similar to Triggers, the system will call the Initialize method. In this method, we bind the attribute class using the AddBindingRule method and bind to the Collector using the AddToCollector method.

Similar to Triggers, when the system starts, it searches for a class that implements IWebJobStartup. When it found a class that implements the interface:

  1. The system calls the Configure method passing the IWebJobsBuilder object. We add the extension using the AddExtension method using the class that implements the IExtensionConfigProvider interface.
  2. The system calls the Initialise method of the IExtensionConfigProvider passing ExtensionConfigContext as a parameter. Our implementation of the Initialize method adds the add the binding rule using the AddBindingRule method of the ExtensionConfigContext, which returns a BindingRule object. We call the BindToCollector to add our binding, passing the Converter as a parameter.
  3. The system calls the Convert method of the ICoverter. Our implementation creates an instance of AsyncCollector class and return.
  4. The systems call AddAsync function to send data to external services.

As stated before, we are creating a NATS extension here. We will use Visual Studio for development. We will also use the NATS client library MyNatsClient. MyNatsClient is a handy library for .NET to connect to NATS.

Custom Extension is a Standard .NET Library. You need to add the following packages using NuGet. Open the NuGet manager and search for:

Creating binding is relatively simple. As mentioned in the previous section, we need to create attribute class, converter class, and async collector class.

Here is our attribute class: using System; using Microsoft.Azure.WebJobs.Description;

Just like in the trigger, we have connection string and channel member variables. Next is our converter class. In this class, we create our async collector instance. Here is our async collector class. Just like in the trigger, we will generate context and pass it to the Collector. The Collector will use this instance to send a message to the NATS server. Here is our collector class.

Create a sample to test the NATS Binding

Let’s create a sample function to test our binding. Our sample function looks like this:

An HTTP call triggers the sample function. We retrieve the message query parameter and send this text to the predefined Channel.

You can use the Postman or other similar tools to test this sample function. I have created a sample node.js application to receive messages from NATS; here is the code.

If everything goes well, you can see the message on the console.

I hope you enjoy this article and got a preliminary knowledge of how to create custom extensions for Azure Functions.

A Software Architect from Kerala, India, Open Source, Cloud Native enthusiast. Likes Golang, Rust, C/C++, Kubernetes, Kafka, etc.