我有一個MVC 4應用程序。它的一個做的事情就是通過調用使用WebClient
像這樣其他服務發送電子郵件:如何等待一個字節[]
var serializer = new JavaScriptSerializer();
string requestData = serializer.Serialize(new
{
EventID = 1,
SubscriberID = studentId,
ToList = loginModel.EmailAddress,
TemplateParamVals = strStudentDetails,
});
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var result = await client.UploadData(URI, Encoding.UTF8.GetBytes(requestData));
}
我想利用UploadDataAsync
但得到的錯誤:
An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle.
於是我想到的創建一個包裝函數&使它異步同時仍然使用UploadDataAsync
。
private async Task<bool> SendEmailAsync(long studentId, LoginModel loginModel)
{
try
{
string URI = ConfigurationManager.AppSettings["CommunicationmanagerURL"] + "PostUserEvent";
var serializer = new JavaScriptSerializer();
string requestData = serializer.Serialize(new
{
EventID = 1,
SubscriberID = studentId,
ToList = loginModel.EmailAddress,
TemplateParamVals = strStudentDetails,
});
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var result = await client.UploadData(URI, Encoding.UTF8.GetBytes(requestData));
}
}
catch (Exception ex)
{
// Log exceptions ...
}
}
這不建,並給出了錯誤:
Cannot await 'byte[]'
任何想法如何解決這個問題?
我有一種感覺,當你嘗試打電話給你的包裝時,你仍然會遇到問題。 – Rawling