[英]How to dispose the Azure Sendgrid Client?
我正在使用Azure Sendgrid發送電子郵件。 如何創建客戶端發送電子郵件。 它沒有一次性接口。
public async Task<string> SendEmailAsync(string EmailTo, string Username, string Subject, string PlainTextContent, string HtmlContent)
{
string apikey = Configuration.AzureSendgridSecretProvider.ApiKey;
string responseStatus = "BadRequest";
var query = "Email_" + EmailTo + ";" + "Username_" + Username + ";" + "VerficationCode_" + HtmlContent+ ";apikey" + apikey;
var client = new SendGridClient(apikey);
try
{
var msg = new SendGridMessage()
{
From = new EmailAddress(Configuration.AzureTableStorageSendGridProvider.EmailAddress, Configuration.AzureTableStorageSendGridProvider.EmailName),
Subject = Subject,
PlainTextContent = PlainTextContent,
HtmlContent = HtmlContent
};
msg.AddTo(new EmailAddress(EmailTo, Username));
var response = await client.SendEmailAsync(msg);
responseStatus = response.StatusCode.ToString();
}
catch (Exception ex)
{
}
return responseStatus;
}
1.我們可以創建單個客戶端來發送所有短信嗎(Singleton)
您可以使用sendgrid的批量電子郵件服務將電子郵件通信發送到多個收件人的大型列表。
2.一旦發送成功,如何調用using {}處理對象?
我反編譯了sendgrid客戶端,發現它依賴HttpClient來處理您的工作站與其API之間的實際Http通信。
http客戶端實現IDisposable接口,該接口處理每個http請求,而sendgrid客戶端沒有一次性實例 。
3.像上面那樣調用時是否有內存泄漏?
隨着時間的流逝,調用郵件api的sendgrid客戶端似乎正在保留內存或正在泄漏內存。
當您嘗試發送成百上千封電子郵件時,您會看到內存使用量不斷增加。無論客戶端堅持使用哪種內存,即使幾天后也再也沒有釋放內存,我們不得不重新啟動服務器以清除內存。
SendGrid客戶端依賴於Microsoft的HttpClient來處理工作站與其API之間的實際HTTP通信。
您可以嘗試實例化單個HttpClient並將其重新用於每個HTTP請求。
有關更多詳細信息,請參閱本文 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.