简体   繁体   中英

How do I make this an asynchronous controller method?

I had a method

    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();
    }

which I need to convert to an asynchronous task because I found out that it was crashing my web app (502 Bad Gateway) whenever I had more than one user actively submitting answers at a time. So I know that I need to do is make this who thing an asynchronous method. Currently, it gets called by my controller like

    [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 });
    }

so I think that what I need to do is make that like

    [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 });
    }

but then what do I do with AddOrUpdateAnswer ? I tried changing it to

public Task AddOrUpdateAnswer

but then I got the error that not all paths return a value. What am I supposed to return?

You just need to convert your method to async:

public async Task AddOrUpdateAnswerAsync ( AnswerSubmission Answer, Guid pid )
    {
        await this._Conn.OpenAsync();
        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);
            await cmd.ExecuteNonQueryAsync();
        }
        this._Conn.Close();
    }

Have a look here for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM