function app can't send message to event grid: The given key '1' was not present in the dictionary

Sergio Solorzano 26 Reputation points
2022-11-25T10:43:18.507+00:00

0

I have a function app with a function that sends message to event grid and a function in this same function app subscribed to this event grid topic. The function app that sends message to event grid uses key 1 to auth.

//Name of the endpoint of Event grid topic  
        string topicEndpoint = transformAlgoSendRMessage_TopicEP;  
        //Creating client to publish events to eventgrid topic  
        AzureKeyCredential credential = new AzureKeyCredential(eventGridTopic_Key);  
        EventGridPublisherClient client = new EventGridPublisherClient(new Uri(topicEndpoint), credential);  
        //Creating a sample event with Subject, Eventtype, dataVersion and data  
        EventGridEvent egEvent = new EventGridEvent("TransformTelemetry", "TransformAlgorithm.broadcastTransform", "1.0", machinePartTransformTelemetry);  
        // Send the event  
          
        try  
        {  
            await client.SendEventAsync(egEvent);  
            log.LogInformation("SendRTransformMessage sent transformdata - PosX:" + machinePartTransformTelemetry[1]);  
        }  
        catch (Exception e)  
        {  
            log.LogError("Failed to send SendRTransformMessage. " + e.Message);  
        }  

Code at execution of await client.SendEventAsync(egEvent) gives me exception error:

The given key '1' was not present in the dictionary.

I get the key using the azure portal copy to clipboard widget and paste in code. I also tried using powershell which confirmed the key.

az eventgrid topic key list --name eventgridtopicname --resource-group myresourcegroup --query "key1"

264174-image.png

I'm using Azure.Messaging.EventGrid v4.12.0 and also tried Microsoft.Azure.Eventgrid v.3.2.0, I'm on VS2022 with latest update.

I note I also tried with RBAC but I received unauthorized 401 error.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,981 Reputation points Moderator
    2022-11-29T05:54:09.61+00:00

    @Sergio Solorzano Thanks for reaching out. I couldn't reproduce the issue at my end. I have used .NET Core 3.1 along with the latest Azure.Messaging.EventGrid v4.12.0 on Visual Studio 2022. I have used the same code that you have shared, and the function execution was successful, and the event was sent correctly to the event grid topic.

    I can only see two values that you are necessary in your above code along with any of the data machinePartTransformTelemetry that you are passing. Please revalidate if they are correct and log the individual values in case if you are getting it from configuration to make sure that the value is passing correctly.

    1. Your variable should have the endpoint value correctly (https://{eventgridtopicname}.{region}-{number}.eventgrid.azure.net/api/events). You can navigate to your event grid topic overview page and check property value of Topic Endpoint
    2. You have already verified the key value as you have mentioned but please make sure that there is no special character, and the value is retrieved correctly for eventGridTopic_Key variable.

    In case you still observe the same behavior then I can connect with you offline to assist you further.

    **Update: **
    The exception was caused by a log statement in the try statement and not related to event grid code.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.