Azure Function V3 .Net Core

Hassan Shabbir 26 Reputation points
2022-02-28T15:37:22.497+00:00

Hello,
i just upgraded my azure function V1 to V3(Core), I am getting some problem with connection string.
Here is function sample

   [FunctionName("Function1")]  
        public static void Run([IoTHubTrigger("messages/events", Connection = "ConnectionStrings:IoTHubEndpoint")] EventData message, ILogger log)  
        {  
            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.EventBody.ToArray())}");  
        }  

I am Getting this error
178554-apperror.png

Here is local.settings.json file.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
},
"ConnectionStrings": {
"IoTHubEndpoint": "mystring"
}
}

Getting this string from here

178555-image.png

Someone guide me please what i am doing wrong here ?

Thank you so much for your response.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Bruno Lucas 4,436 Reputation points MVP
    2022-03-01T00:56:22.743+00:00

    Hi, I tried your code on a v3 function and that worked for me.

    Looking on your error looks like your connection string is not correct.

    If you copy from here

    178590-image.png

    Notice a trigger need a path to a hub. that means you need to replace the "mystring" in ("IoTHubEndpoint": "mystring") with the whole value of the endpoint

    that will look like this (don't forget the EntityPath). Look like your error is complaining about the EntityPath bit:

    Endpoint=sb://iothub-ns-xxxx-17640743-b3xxx6c.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=oxxxyU2B0Hcxm9d9aS5EbkxxxXM8/8teIGuro=;EntityPath=myotakl990

    That error is common for sender code. if you add the full connection string including the path you should see that error . for sender with 2 params, one is the entity path and the other is the conn string without the path.
    178663-image.png

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 36,951 Reputation points MVP Volunteer Moderator
    2022-02-28T20:36:39.193+00:00

    Hello @Hassan Shabbir ,

    I just created a test project based on your information (using VS Code and azure function template).

    I was able to run it locally and ingest messages from an IoT Hub:

    178585-image.png

    I used this configuration:

    {  
      "IsEncrypted": false,  
      "Values": {  
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",  
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"  
      },  
      "ConnectionStrings": {  
        "IoTHubEndpoint": "Endpoint=sb://iothub-ns-storage-ro-17651825-0b2daeb2a7.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=[a sas key]=;EntityPath=storage-route-test-ih"  
      }  
    }  
    

    With this function:

    using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;  
      
    using Microsoft.Azure.WebJobs;  
    using Microsoft.Azure.WebJobs.Host;  
    using Microsoft.Azure.EventHubs;  
    using System.Text;  
    using System.Net.Http;  
    using Microsoft.Extensions.Logging;  
      
    namespace Company.Function  
    {  
        public static class IoTHub_EventHub1  
        {  
            private static HttpClient client = new HttpClient();  
      
            [FunctionName("IoTHub_EventHub1")]  
            public static void Run([IoTHubTrigger("messages/events", Connection = "IoTHubEndpoint", ConsumerGroup = "fa")]EventData message, ILogger log)  
            {  
                log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");  
            }  
        }  
    }  
    

    The only thing a changed was adding a consumer group.

    I'm not sure why you get that error message. As you can see, it works for me.

    Full code example available at GitHub.

    1 person found this answer helpful.

  2. MughundhanRaveendran-MSFT 12,511 Reputation points
    2022-03-01T05:52:12.047+00:00

    @Hassan Shabbir ,

    Thanks for reaching out to Q&A.

    The connection string should be passed in this way Connection = "IoTHubEndpoint". Create a name value pair in the local.settings.json file with the name "IoTHubEndpoint" and connection string obtained from the portal as value.

    I hope this helps!

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.


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.