所以我有一些奇怪的事情发生了。我正在尝试让SendGrid v9在我的网络应用上工作。我通过一系列测试来运行它,包括创建一个控制台应用程序进行测试。它在控制台应用程序中运行良好,但挂在Web应用程序中。我显然错过了一些东西 - 可能与异步调用有关,但真的在寻找帮助:
我的控制台应用:
namespace Example
{
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Collections.Generic;
using System.Diagnostics;
internal class Example
{
private static void Main()
{
Execute().Wait();
}
static async Task Execute()
{
var client = new SendGridClient("[MYKEY]");
// Send a Single Email using the Mail Helper
var from = new EmailAddress("my@email.com", "Example User");
var subject = "Hello World from the SendGrid CSharp Library Helper!";
var to = new EmailAddress("my@email.com", "Example User");
var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!";
var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
Debug.WriteLine(msg.Serialize());
Debug.WriteLine(response.StatusCode);
Debug.WriteLine(response.Headers);
}
}
}
我的网络应用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace WebAppTest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Execute().Wait();
}
public static async Task Execute()
{
var client = new SendGridClient("[MYKEY]");
// Send a Single Email using the Mail Helper
var from = new EmailAddress("my@email.com", "Example User");
var subject = "Hello World from the SendGrid CSharp Library Helper!";
var to = new EmailAddress("my@email.com", "Example User");
var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!";
var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg); //It hangs on this call
Debug.WriteLine(msg.Serialize());
Debug.WriteLine(response.StatusCode);
Debug.WriteLine(response.Headers);
}
}
}
这些是空白项目,在web.cofigs或app.configs
中没什么我缺少什么。为什么Web应用程序无法正常运行,但控制台应用程序确实如此?
TIA