如果沒有結果,則不要嘗試返回結果。只是返回平原,完成Task
:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
new Email().Send(message);
return Task.CompletedTask;
}
}
如果你是停留在前期4.6的土地,那麼你可以使用Task.FromResult<bool>(true)
代替。
所有這一切說,我對你的評論感到困惑「async...await
是不兼容using
」。根據我的經驗,這很好。如果你的方法實際上是異步的,它會好得多。我認爲你應該把注意力放在如何做到這一點上,而不是用什麼最好的/正確的語法來僞造一個方法。
附錄:
我現在還不能對你對使用using
關注清晰。但是,根據您的評論,似乎您想使用SmtpClient.SendAsync()
,但不確定如何將其應用於async
/await
的上下文中。
不幸的是,即使在.NET之前,我們有很多異步方法,並且這些方法和新的等待方法使用相同的命名規則。 (要清楚:這是不幸的命名,不是異步方法存在:))。但是,在所有情況下,都有可能將舊的API適用於新的API。
在某些情況下,這與使用Task.FromAsync()
方法一樣簡單。這適用於任何支持舊模型的舊模型。但SmtpClient.SendAsync()
模型是基於事件的回調方法,需要稍微不同的方法。
注意:在寫下例子後,我注意到SmtpClient
類有一個基於Task
的異步操作方法SendMailAsync()
。所以實際上,沒有必要調整舊的SendAsync()
方法。但是,這是一個很有用的例子,用於說明在沒有提供基於Task
的替代方案時,如何進行此類改編。
簡單地說,你可以使用TaskCompletionSource
與SmtpClient
對象的SendCompleted
事件。這裏是什麼看起來像一個輪廓:
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
// I'm not familiar with "IdentityMessage". Let's assume for the sake
// of this example that you can somehow adapt it to the "MailMessage"
// type required by the "SmtpClient" object. That's a whole other question.
// Here, "IdentityToMailMessage" is some hypothetical method you write
// to handle that. I have no idea what goes inside that. :)
using (MailMessage mailMessage = IdentityToMailMessage(message))
using (SmtpClient smtpClient = new SmtpClient())
{
TaskCompletionSource<bool> taskSource = new TaskCompletionSource<bool>();
// Configure "smtpClient" as needed, such as provided host information.
// Not shown here!
smtpClient.SendCompleted += (sender, e) => taskSource.SetResult(true);
smtpClient.SendAsync(mailMessage, null);
await taskSource.Task;
}
}
}
以上將啓動異步操作,並使用SendCompleted
事件的處理程序(即「回調」到該文件指)來設置結果爲TaskCompletionSource<bool>
對象(結果值從來沒有真正使用,但沒有純香草Task
版本的TaskCompletionSource
&hellip;你必須有一些值)。
它採用await
,而不是直接返回taskSource.Task
對象,因爲這樣它可以正確處理處置的SmtpClient
對象的電子郵件操作實際上已經完成時。
酷似顯示在http://stackoverflow.com/questions/16566547/do-using-statements-and-await-keywords-play-nicely-你不喜歡'using'和'async'(什麼在-C-尖銳)? –
@AlexeiLevenkov:我發現有些例子說這不是有效的代碼,我看到'SmtpClient.SendAsync()'帶有第二個參數,它與回調相關。這就是我想要避免的。 –
OT:[SmtpClient文檔](https://docs.microsoft.com/dotnet/api/system.net.mail.smtpclient)建議使用[MailKit](https://github.com/jstedfast/MailKit)和[MimeKit](https://github.com/jstedfast/MimeKit)。 –