我有一個方法如何使這個異步控制器方法?
public void AddOrUpdateAnswer (AnswerSubmission Answer, Guid pid)
{
this._Conn.Open();
using (SqlCommand cmd = new SqlCommand("AddOrUpdateAnswer", this._Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@AnswerVal", Answer.AnswerVal);
cmd.Parameters.AddWithValue("@QuestionId", Answer.QuestionId);
cmd.Parameters.AddWithValue("@PartnerId", pid);
cmd.ExecuteNonQuery();
}
this._Conn.Close();
}
,我需要轉換成一個異步任務,因爲我發現它被撞毀我的web應用程序(502網關錯誤),每當我在有一個以上的用戶主動提交答案時間。所以我知道我需要做的是讓這個人成爲一個異步方法。目前,它就會由我控制像
[HttpPost]
public ActionResult SubmitAnswer (AnswerSubmission Answer, Guid pid)
{
bool goodSoFar = true;
string status = "Answers submitted successfully";
try
{
this._Db.AddOrUpdateAnswer(Answer, pid);
}
catch (Exception e)
{
goodSoFar = false;
status = String.Format("Exception occured during answer submission: {0}", e.Message);
}
return Json(new { Succeeded = goodSoFar, Message = status });
}
所謂我認爲我需要做的是使該 像
[HttpPost]
public async Task<ActionResult> SubmitAnswer (AnswerSubmission Answer, Guid pid)
{
bool goodSoFar = true;
string status = "Answers submitted successfully";
try
{
await this._Db.AddOrUpdateAnswer(Answer, pid);
}
catch (Exception e)
{
goodSoFar = false;
status = String.Format("Exception occured during answer submission: {0}", e.Message);
}
return Json(new { Succeeded = goodSoFar, Message = status });
}
但後來我該怎麼辦與AddOrUpdateAnswer
?我試圖將其更改爲
public Task AddOrUpdateAnswer
但後來我得到了,並不是所有的路徑返回一個值的誤差。我應該回來什麼?
'async'不會改變你的web應用程序的語義。如果你得到一個帶有同步代碼的502,那麼你將得到一個帶有異步代碼的502。 –