I'm not saying it was built with Azure Functions, but it was definitely built with Azure Functions.

How to fix “Microsoft.WindowsAzure.Storage: Server encountered an internal error. Please try again after some time.” when using IoTHub trigger for Azure Functions?

This post was most recently updated on January 12th, 2022.

2 min read.

One more article before Christmas! 😁 This time, I’ll be fixing a function that’s triggering based on new messages received through my IoT Hub in my Azure Functions project. Technically speaking, this is super simple to implement – your function needs to define an IoTHubTrigger, with an Event Hub name and usually a Connection String (I don’t know why I’m Sentence Casing that, too, but I’ll stick to it!)

You can tap a practically unlimited number of Azure Function triggers to any Event Hubs (or Azure IoT Hubs), right? Well – yeah, that’s what it looks like, but actually, you can’t. Not without an additional configuration step, that is!

But before getting into the configuration, let’s take a better look at the actual error we’re running into!

Problem

So while trying to run your Azure Functions project with debugging enabled, you’ll run into an annoying issue where the function fails to start and throws an error somewhat like below:

[2021-07-02T09:55:05.076Z] EventProcessorHost error (Action='Checking Leases', HostName='8cc55fad-d313-434d-b663-2b96bbc9efa1', PartitionId='1').
[2021-07-02T09:55:05.078Z] Microsoft.WindowsAzure.Storage: Server encountered an internal error. Please try again after some time.
[2021-07-02T09:55:08.673Z] Host lock lease acquired by instance ID '000000000000000000000000D3CDE7D8'.

Or in the CLI itself:

What do?

Reason

Your Azure Functions are trying to tap into an IoT Hub to fire on an EventHubTrigger – and failing due to a conflict in Consumer Groups. See – each Azure Function trigger essentially needs a separate Consumer Group.

Yeah, it’s an absolutely abysmal error message. But that’s ok – you’re here now, and I’ve got the solution! ;)

Actually, I’ve got 2 solutions (because it could also be your storage emulator messing up). So listen up.

Solution

Below, I’m outlining the steps that have always helped me to fix this particular issue. The first step is just to restart your Azure Storage Emulator/Explorer (if you’re using one), and if that doesn’t help, (re)configure your IoT Hub’s Consumer Groups.

That said, let’s get to it!


Time needed: 30 minutes.

How to fix “Microsoft.WindowsAzure.Storage: Server encountered an internal error.”?

  1. Restart your Azure Storage Explorer

    Let’s get the obvious one first – if this works, you don’t need to go through all the rest of the steps!

    If your Azure Storage Explorer (or Azurite, if that’s what you’re using) is borked, restart it. Maybe it’ll fix the issue and you’ll be good!

    It didn’t? Thought it might not – check out the next steps, then!

  2. Create a new Consumer Group for your Iot Hub

    Navigate to Azure IoT Hub, and “Built-in Endpoints” under it. On that blade/tab, you should see something like below – create a new consumer group by entering a new alphanumeric value (hyphens supported) in the field and saving the changes.

    This image has an empty alt attribute; its file name is image-10.png

  3. Add a new application setting for your Consumer Group

    Now you’ll need to tell your Azure Function the name of the consumer group you want it to use.

    Something like this:
    "Values": {
    "AzureIoTHubConsumerGroupName": "MyCustomConsumerGroup"
    }

  4. Add the “ConsumerGroup” parameter to your trigger (if it doesn’t exist already)

    In code, this will look somewhat like below:

    public static Task Run(
    [IoTHubTrigger("events",
    Connection = "EventHubConnectionAppSetting",
    ConsumerGroup = "%AzureIoTHubConsumerGroupName%"
    )] EventData myEventHubMessage)

And that should be it! Did it resolve your issue? Still having issues? Let me know in the comments -section below!


References

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments