c# - Azure 函数不运行异步方法
问题描述
我有一个不在 Azure 中运行的 Azure 函数。它在本地运行,但在 Azure 中不运行。我有一个 try/catch,它没有捕获任何异常,但在日志中我得到“2020-07-26T12:23:00.021 [错误] 发生异常。” 不明白我做错了什么。
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);
}
}
}
}
这是我得到的日志:
2020-07-26T12:35:00.007 [信息] C# 定时器触发函数执行于:7/26/2020 12:35:00 PM
2020-07-26T12:35:00.030 [错误] 发生异常。
2020-07-26T12:35:00.045 [信息] 执行“SendEmailFunction”(成功,Id=8b5d1b47-37de-4f84-8936-d31b19f0f73d,持续时间=42ms)
解决方案
克里特纳的回答很有帮助,因为它让我看到了例外。
这是因为它没有找到我从中读取值的文件。我通过构造这样的路径来解决这个问题: var path = Path.Combine(context.FunctionAppDirectory, "1.csv");
其中 context 是 Microsoft.Azure.WebJobs 命名空间中的 ExecutionContext。
现在已经修好了。
谢谢!
推荐阅读
- php - laravel 无法打开流:没有这样的文件或目录错误
- c# - 在ajax成功中更新表的每个td值
- python - WebElement 方法未显示
- html - 内联 SVG
与矢量效果结合时在 Firefox 中中断:非缩放笔画 - node.js - 在 Sequelize 中隐藏列名
- android - 将自己的字体添加到 android 项目
- angular - 如何将数据组合到不同类型的对象数组并对其进行迭代以创建表行
- python - 想要以编程方式为“媒体文件”添加 git 以从 views.py 跟踪它们的历史记录
- python - TypeError:“str”对象不能通过 Python 使用 Selenium 调用
- php - 电子邮件动态内容与 foreach 内的第一个循环值重复