我有一个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