SendGrid Azure function runs locally but not in Azure

Doug Marsh 1 Reputation point
2022-10-28T22:38:16.657+00:00

I have a queue triggered Azure function and it works perfectly locally. Emails are retrieved from the queue and sent when run locally. However when I publish from Visual Studio it does run. When I test in the portal it returns 404 Not found. I have both my SendGridApiKey and CloudStorageAccount setup in Application Settings, and they match what is in my local.settings.json file. I'm using an email from my SendGrid Verified domain. The sister Azure function that loads an email message into the queue run and executes fine so it's recognizing the cloudstorageaccount.
I've been following this example:

https://www.c-sharpcorner.com/article/how-to-send-email-using-sendgrid-with-azure-function-in-net-core2/

I might add that I wrote a simple sendgrid function in the portal and it works. I'm posting my code below but I don't think it's code related but rather a configuration setting or binding. I'm stumped. Please help.

My Code:

namespace AzFunctions
{
public static class SendGridEmailQueueTriggerFunction
{
[FunctionName("SendGridEmailQueueTriggerFunction")]
public static void Run([QueueTrigger("email-queue", Connection = "CloudStorageAccount")] string myQueueItem,
[SendGrid(ApiKey = "SendGridApiKey")] out SendGridMessage sendGridMessage,
ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        try  
        {  
            var queueItem = myQueueItem.ToString();  

            dynamic jsonData = JObject.Parse(queueItem);  
            string emailBody = jsonData.Content;  

            log.LogInformation("EmailBody: "+ emailBody);  

            sendGridMessage = new SendGridMessage  
            {  
                From = new EmailAddress("Orders@TimePilotCloud.com", "Order Updates"),  
            };  
            sendGridMessage.AddTo("xyz@zzzz.com");  
            sendGridMessage.SetSubject("Awesome Azure Function app");  
            sendGridMessage.AddContent("text/html", "Should Be Email Body");  
        }  
        catch (Exception ex)  
        {  
            sendGridMessage = new SendGridMessage();  
            log.LogError($"Error occured while processing QueueItem {myQueueItem} , Exception - {ex.InnerException}");  

        }  
    }  
}  

}

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,978 questions
Azure Queue Storage
Azure Queue Storage
An Azure service that provides messaging queues in the cloud.
105 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruno Lucas 4,431 Reputation points MVP
    2022-10-29T02:33:50.103+00:00

    Hi @Doug Marsh

    On your code you have a queue trigger function. how do you run that to see the 404 error? to test you should send e message to the queue and see if the trigger will fire. The example mentions one queue trigger and one http trigger. Which once throws 404?

    404 sounds like Visual Studio failed to fully deploy the function. Is your Visual Studio up to date?
    Do you get any message during publishing?

    Can you open the function app after deployment and stop-start?
    255228-image.png

    Is the function version correct to the Function App version?
    Can you see anything under the execution logs or insights?
    Does the Function App start at all?

    255218-image.png

    if you check under kudu, if you only have that it means VS failed to deploy:

    255285-image.png

    0 comments No comments

  2. Doug Marsh 1 Reputation point
    2022-10-31T14:55:09.767+00:00

    Thanks for the response.

    • I test the function by putting a message in the associated storage queue by sending a message to the http triggered azure function. The message appears in the queue. So the http trigger functions works perfectly. There is no response in Application Insights. I get the 404 not found message by simply clicking Code + Test in the send email queue triggered function. I realize that's probably not a valid, but it's a response. Running locally both functions work perfectly and messages go in and out of the storage queue.
    • The function Starts and stops from the portal but there is no output to Application Insights from the Log.loginformation statement which is the first line of code in that function.
    • Visual Studio 2022 is up to date.
    • The only output from Application Insights is: "Connected!
      2022-10-31T14:47:10 Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds)."
    • Yes. The function version is correct to the Function App Version
    • There is no message when I publish other than: "========== Publish: 1 succeeded, 0 failed, 0 skipped ==========
      Waiting for Function app to be ready..."

    It smells like it's missing a dependency after it's publish but I don't know how to diagnose this. Your help is appreciated.
    Function app is ready


  3. Doug Marsh 1 Reputation point
    2022-11-01T18:35:24.563+00:00

    When I add a file to the queue i get nothing. Not even an error.

    256130-screenshot-2022-11-01-132103.jpg

    in the bin folder the files do match

    256040-image.png

    The http trigger function that uploads messages into the queue works fine.

    Any ideas? What am I missing? I do have an http triggered sendgrid function that works that I wrote in the Azure portal, but I want to work within visual studio which I've used to develop dozens of azure functions without any problems. The only thing different is the Sendgrid, however all of the files seems to be there including the Sendgrid.dll.


  4. Doug Marsh 1 Reputation point
    2022-11-04T14:54:26.743+00:00

    Sorry for the slow response. I get zero logging. I have also reduced my code to just single log.Information statement and still no response. If I develop a new app and start with a single Log.Information statement it works. If I add back the code it stops working with the SendGrid reference. If I remove the sendgrid statement and it's references it still doesn't function...meaning no output to the output window.

    I now have a completely working function but I had to develop it in the portal. I'd much rather work in visual studio, but I can't figure it out. I checked the app settings and the Function.json file and everything matches between the visual studio developed app and the portal developed app. I've developed dozens of Azure functions successfully using visual studio but this one has me stumped. All of the previously developed functions I was using VS 2019, where I'm now using VS2022.


Your answer

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