简体   繁体   中英

Azure function doesn't run async method

I have an Azure Function that doesn't run in Azure. Locally it runs, but in Azure it doesn't. I have a try/catch in place, which doesn't catch any exception, but in the logs I get "2020-07-26T12:23:00.021 [Error] An exception occured." Don't understand what I'm doing wrong.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.Core.Models;
using FluentEmail.Mailgun;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace SendEmailFunction
{
    public static class SendEmailFunction
    {
        private const string EmailSubject = "Collaboration proposal";

        [FunctionName("SendEmailFunction")]
        public static async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            try
            {
              var path = Path.Combine(Directory.GetCurrentDirectory(), "1.csv");
              List<string> lines = (await File.ReadAllLinesAsync(path)).ToList();
            }
            catch (Exception ex)
            {
                log.LogError($"An exception occured.", ex);
            }
        }
    }
}

This are the logs that I get:

2020-07-26T12:35:00.007 [Information] C# Timer trigger function executed at: 7/26/2020 12:35:00 PM

2020-07-26T12:35:00.030 [Error] An exception occured.

2020-07-26T12:35:00.045 [Information] Executed 'SendEmailFunction' (Succeeded, Id=8b5d1b47-37de-4f84-8936-d31b19f0f73d, Duration=42ms)

Kritner's answer helped because it permited me to see the exception.

It was because it didn't find the file from which I read the values. I fixed that by constructing the path like this: var path = Path.Combine(context.FunctionAppDirectory, "1.csv");

Where context is ExecutionContex from Microsoft.Azure.WebJobs namespace.

It's fixed now.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM