Saturday, 1 February 2020

Azure function bindings


Bindings provide a way to read/write data from/to different data services without any additional overhead of managing connection with these services. Today, we will see that how easy it is to integrate Azure functions with different services like blob, table storage, queue, cosmos, send grid etc with help of bindings.

Type of bindings –
  1. Input binding – As the name implies, the binding is to get data into an Azure function from data service like blob, table storage, cosmos etc.
  2. Output binding – The binding is to push/add data to different data service from an Azure function like blob, queue, send grid etc.

Demo – We will create a Todo API with following,
  • An azure function with Add/List Api,
  • Output binding - Data will be stored in a Table Storage.
  • Output binding – We will write the data in a text file and store it in a blob storage.
  • Input binding – Read data from table storage to display on UI.

·         I will be using a simple angular page to add/list todos but it could be a simple html page with some ajax or use any client like postman to hit the API directly.

I will be using Visual Studio but it is absolutely fine using any IDE like VS Code if want to follow along or refer my previous post getting started with azure functions

Creating API - Lets create the API first by following the steps below and then will move on to UI.
  1. Setup new azure function project with an empty template as shown below,
  2. Your solution should look like this,
  3. Add new azure function by following the steps below,
  4. Add required nuget package by running following command in package manager console,
    • Install-Package Microsoft.Azure.WebJobs.Extensions.Storage
  5. Add new class Models.cs and add following,
    • Todo POCO
    • TodoEntity to be used while working with storage table
  6. Add two endpoints CreateTodo/Todos in API to create and read data.
    • Include all necessary namespaces
    • CreateTodo endpoint to create a new todo item
    • Todos endpoint to get all todos for be displayed
  7. Now hit the API by using an angular page or java script then It should give you an error like below,
  8. And this is because we are trying to access an API of different domain from our client App. Now allow cross domain communication by enabling CORS under host in local.settings.json like this,
  9. You should now have a working azure function with added input/output bindings.

No comments:

Post a Comment