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

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
      New_AzureFunction
    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
      Azure_Function_Template
    9. Now we need to add ServiceBus Connection String
    10. Click on new link next to the Service Bus Connection as shown below
      Azure_Function_Connectstring
    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
      Azure_Function_Code
    13. Enter below code in the run.csx file

      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; }
      }

    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
      {
          "frameworks": {
              "net46": {
                  "dependencies": {
                      "StackExchange.Redis":"1.1.603",
                      "Newtonsoft.Json": "9.0.1"
                  }
              }
          }
      }

      Azure_Function_Projectfile

    17. Now try to run the Function, click on the Run button in the Run section as shown below
      Azure_Function_Run
    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
      Redis_Console 

41 thoughts on “Azure Function Apps – reading events from ServiceBus and writing to Redis Cache

Add yours

  1. Wonderful site you have here but I was curious if you knew of any message boards that cover the same topics
    discussed in this article? I’d really love to be a part of
    community where I can get opinions from other knowledgeable individuals that share the same interest.

    If you have any recommendations, please let me know.

    Cheers!

    Like

  2. I absolutely love your blog.. Pleasant colors & theme.
    Did you create this amazing site yourself? Please reply back as I’m planning to create my own personal site and would love to learn where you got this from or
    what the theme is named. Cheers!

    Like

  3. I absolutely love your blog and find the majority of your post’s to be exactly I’m looking for. Do you offer guest writers to write content available for you? I wouldn’t mind creating a post or elaborating on many of the subjects you write about here. Again, awesome web log!|

    Like

  4. I’m no longer sure where you are getting your information, however good topic. I must spend a while finding out more or understanding more. Thank you for fantastic info I was looking for this information for my mission.

    Like

  5. Attractive element of content. I simply stumbled upon your weblog and in accession capital to assert that I acquire in fact loved account your weblog posts. Anyway I will be subscribing on your feeds and even I achievement you access consistently fast.|

    Like

  6. I think other website proprietors should take this site as an model, very clean and excellent user genial style and design, let alone the content. You are an expert in this topic!

    Like

  7. Hi there! I’m at work surfing around your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the great work!|

    Like

  8. Hello just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.

    Like

  9. I don’t know whether it’s just me or if everyone else experiencing issues with your website. It seems like some of the written text within your content are running off the screen. Can somebody else please comment and let me know if this is happening to them too? This might be a problem with my browser because I’ve had this happen previously. Many thanks

    Like

  10. Have you ever considered publishing an ebook or guest authoring on other sites? I have a blog based on the same ideas you discuss and would really like to have you share some stories/information. I know my subscribers would enjoy your work. If you’re even remotely interested, feel free to shoot me an e mail.

    Like

  11. Hi would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you suggest a good web hosting provider at a fair price? Thanks a lot, I appreciate it!|

    Like

  12. Hey there, I think your blog might be having browser compatibility issues. When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, very good blog!|

    Like

Leave a comment

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

Create a free website or blog at WordPress.com.

Up ↑