【发布时间】:2020-12-24 22:20:54
【问题描述】:
我正在设置一个发送邮件的功能,但出现以下异常:
"message": "Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.",
"type": "System.InvalidOperationException",
"source": "System.Web",
函数如下:
public async void r102Implementation(ActivitiesModel instance)
{
var apiKey = Environment.GetEnvironmentVariable("API_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("account@com.com", "Example User");
var subject = "Sending with Twilio SendGrid is Fun";
var to = new EmailAddress("account@com.com", "Example User");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
}
【问题讨论】:
-
async void通常只能由事件处理程序使用。您是否尝试让该方法返回async Task? -
阅读错误信息。它字面意思告诉你该做什么。
标签: c#