0
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Contact(LoginViewModel objmodel, string returnUrl)
    {
        LoginViewModel model = new LoginViewModel();

        if (!ModelState.IsValid)
        {
            return View(model);
        }

        string mode = "User_Login";
        //string userLoginName = "test";
        //string userPassword = "test";
        string userLoginName = model.UserName;
        string userPassword = model.Password;

        ObjectParameter returnParams = new ObjectParameter("Params", typeof(string));
        DBECRF_01Entities3 ent = new DBECRF_01Entities3();

       string outparam = (ent.Sp_UserLogin(mode, userLoginName, userPassword, returnParams)).ToString();

        switch (outparam)
        {
            case "Success":
                return View("Contact");
            case "Failure":
                return View("Lockout");
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }

       // return View("Index", model);
    }

This is My Controller method where I want to implement the async method. But unfortunately it gives me a fatal warning:

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Like this.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109

1 Answers1

0

If you have to create an asynchronous method, you indeed need asynchronous content in it (a call that will require an unknown amount of time to be executed) like a database call for instance, and use the word await in front of it (ad your error message suggests):

public async Task<Stuff> GetStuff()
{
   return await db.GetAllStuff();
}

If you can't have any asynchronous call inside your method, then the best practive is "not to lie" and leave it as synchronous: in your case, replace

async Task<ActionResult>

with

ActionResult

But if you REALLY need to, you can add this line:

await Task.Delay(100);

100 being the time to wait in ms.

Brocodile
  • 23
  • 6