Azure Function Apps – reading events from ServiceBus and writing to Redis Cache




First published on MSDN on Sep 23, 2016



This blog is part of IoT Series, where I am trying to build few IoT devices that push events to Azure EventHub.  From the EventHub, Azure Stream Analytics will execute my query to calculate average values for each individual device and publish these average values to Azure ServiceBus. From Azure ServiceBus, I am going to read the average values in Azure Functions Apps and save them into Azure Redis Cache. My Azure Website will poll this Redis Cache and displays the average values.




Here are list of blog posts in this series:










    1. Azure IoT






    2. Azure EventHub–sending IoT device events to EventHub






    3. Azure ServiceBus Queue–reading query results from Stream Analytics






    4. Azure Stream Analytics–reading events from EventHub, running query and saving results to ServiceBus





    5. Azure Function Apps – reading events from ServiceBus and writing to Redis Cache









In this blog, I am going to show how to configure Azure Function Apps to read from Azure ServiceBus and write them to Azure Redis Cache.









    1. Log into Azure Portal




    2. Click on

      + New

      button




    3. In the Search, type

      Function App





    4. Select

      Function App

      and provide name, resource group, storage account as shown below







    5. Click on

      Create

      button




    6. Once the Function App is deployed, browse to newly created Function App




    7. In the Function App, click on the

      + New Function





    8. In the templates, select

      ServiceBusQueueTrigger – C#

      as shown below







    9. Now we need to add ServiceBus Connection String




    10. Click on

      new

      link next to the

      Service Bus Connection

      as shown below







    11. Provide the connection string name and connection string to Azure Service Bus. Click here to learn how to get connection string.




    12. Expand function and click on

      Develop

      node as shown







    13. Enter below code in the

      run.csx

      file


      [code language=”csharp”]


      using System;


      using System.Threading.Tasks;


      using StackExchange.Redis;


      using Newtonsoft.Json;




      public static void Run(string myQueueItem, TraceWriter log)


      {


      log.Info($”C# ServiceBus queue trigger function processed message: {myQueueItem}”);




      var msg = JsonConvert.DeserializeObject<MyOutput>(myQueueItem);




      if (msg == null)


      {


      log.Info(“failed to convert msg to MyOutput”);


      return;


      }




      IDatabase cache = Connection.GetDatabase();


      cache.StringSet(msg.devicename, myQueueItem);


      }




      private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>


      {


      return ConnectionMultiplexer.Connect(“redis connection string”);


      });




      public static ConnectionMultiplexer Connection


      {


      get


      {


      return lazyConnection.Value;


      }


      }




      class MyOutput


      {




      public string devicename { get; set; }


      public double avgtemp { get; set; }


      public double avgspeed { get; set; }


      }


      [/code]








    14. Now we need to add NuGet package for Newtonsoft and Redis Cache




    15. Click on the

      View files

      and click

      +

      to add a new file




    16. Set the name of the new file as

      Project.json

      and add these lines


      [code language=”json”]


      {


      “frameworks”: {


      “net46”: {


      “dependencies”: {


      “StackExchange.Redis”:”1.1.603″,


      “Newtonsoft.Json”: “9.0.1”


      }


      }


      }


      }


      [/code]







    17. Now try to run the Function, click on the

      Run

      button in the

      Run

      section as shown below







    18. Check for any compiler errors in the above

      Logs

      section




    19. next, push few events into EventHub using the console app, code is

      here





    20. These events will travel from EventHub to Stream Analytics to Service Bus Queue to Function App to Redis Cache




    21. To verify if the event reached Redis Cache, in the Azure Portal, navigate to Redis Cache and click on Console and execute these commands as shown









Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.