>> Learn how to create custom bindings for
Azure Functions in this episode of Azure Tips and Tricks.
Azure function bindings are great.
They help you to communicate with
services without writing the plumbing to connect to them,
and if you need a binding that doesn't come out of the box,
you can create a custom one.
I've already created a custom binding in this class library.
You can download the source code for
the sample application at this address.
To create a custom binding,
you should create a.NET standard class library,
which you can use in your Azure Function.
Let's go through it to see how you can create a custom binding.
This is the binding itself.
The binding is able to read the contents from files.
It is a class that has the extension attribute on it
and that implements the i extension conflict provider interface.
Because of this, it has the initialized methods which are used to
add a binding rule for the MyFile reader binding attribute.
This binds input values that are
defined in the Azure Function to the binding.
Let me show you. This is the attribute,
it has been tagged as a binding and
it has one property which is the location.
This will contain the location of the files to read,
so something like C-Temp,
and this has the outer resolve attribute
which means that it will try and fill the location parameter
with a value from the configuration system like
one that we store in local.settings.json.
So that's the attributes and
this is the File Reader model that we use.
This contains the full file path and content,
which is the content of the file.
This rule says that we fill the model with
this method built item from attribute.
This does the actual work.
It gets the arcs which is the attribute containing your location
and it uses that to see if the file exists and if it does,
read its contents and it then takes the location and
content and returns that as the MyFile Reader Model.
This is the custom binding.
To make it work, there is some plumbing like this class.
This initializes the custom binding
when the Azure function starts up.
It still references web jobs because Azure Functions
are built on web jobs and are the evolution of them.
It then uses an extension method which
is this one to initialize the custom binding.
Now to the function,
this function project references
the class library of the custom binding.
It is a function that is triggered by HTTP,
so I can just call it from a browser and it has a route
defined that takes a name parameter
which is the name of the file that we want to read.
It also has the MyFile Reader binding here which has
the input parameter of location which is
made up of file path and name.
Once it has read the file,
it simply returns the content
and the file path is defined in here.
In my case, it is the test folder on my C-Drive and that is here.
Here I have created two simple text files with some text in them.
Let's try it out. I'll build it and run it, and here it is.
This is a console that shows
my Azure Function running locally at this URL.
Let's try that out in the browser.
As it takes a name parameter,
all I need to do is add the name of the document,
there it is, the content of document A.
Now let's try to get the content of
document B and there it is, it works.
Custom bindings for Azure functions are really useful because they
allow you to make Azure functions work for your specific scenario.
Go and check it out.