[英]Create new HttpContext for MvcMailer so I can use async/await
我正在使用MVCMailer,它依賴於HttpContext來發送郵件。
在某些情況下,我是從WebJob發送郵件,或者是在調用ConfigureAwait(false)
的流中使用的異步任務,所以在我點擊MVCMailer代碼之前我丟失了上下文。
我的選擇似乎是:
ConfigureAwait(false)
(我的許多服務在某些時候都使用Mailing,因此其中一個實例將刪除我需要的上下文) 我正在尋找快速簡便的解決方案 - 如果需要,可以新建一個HttpContext。 這可能嗎?
這是我現在使用的代碼,它依賴於HttpContext:
public class UserMailer : MailerBase {
public UserMailer() {
MasterName = "_Layout";
}
public virtual MvcMailMessage Message(MailMessage message, string unsubscribeLink = null) {
ViewBag.Body = message.Body;
ViewBag.Subject = message.Subject;
ViewBag.UnsubscribeLink = unsubscribeLink;
return Populate(x => {
x.ViewName = "Message";
});
}
}
如果MvcMailer必須有一個HttpContext引用,我會抽象它。 創建一個公共郵件接口或基類(看起來你現在已經使用MailerBase),如果你有:
public class WebJobMailer : MailerBase { .. }
然后你可以讓webjob使用這個實現,但你的MVC應用程序是MvcMailer實現...... WebJob郵件程序可以回退到System.Net.Mail,或者使用其他東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.