3

I am trying to learn the details of MVC5 and I came across something that baffles me. In the accountController that comes by default with an MVC5 project with Individual Auth there is this line of code in the async Login and Register methods

var result = await UserManager.CreateAsync(user, model.Password);

I read here http://msdn.microsoft.com/en-us/library/hh191443.aspx that this is a normal practice, but I do not understand why you would ever use an asynchronous method and await in the same line. Wouldn't it make more sense to just use the .Create method that takes the same parameters here?

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
esastincy
  • 1,607
  • 8
  • 28
  • 38
  • http://stackoverflow.com/questions/4057359/new-c-sharp-await-feature – maxbeaudoin Dec 21 '13 at 03:57
  • http://stackoverflow.com/questions/4054263/how-does-c-sharp-5-0s-async-await-feature-differ-from-the-tpl?rq=1 – maxbeaudoin Dec 21 '13 at 03:58
  • What I am getting out of the second post is that it will spin up a new thread to do the .CreateAsync instead of using the current one. Is that it? Is there any real value in that? – esastincy Dec 21 '13 at 04:09
  • 1
    There is no new thread involved. It simply make better use of the current thread. See http://stackoverflow.com/questions/19087513/what-is-the-advantage-of-using-async-with-mvc5 – maxbeaudoin Dec 21 '13 at 04:15
  • Thank you that explained it perfectly – esastincy Dec 21 '13 at 04:20

1 Answers1

2

The difference between SomeMethod() and await SomeMethodAsync() is that the latter won't block a thread while the method performs IO. Because of that, the application becomes more scalable, because it can use smaller number of threads to serve the same number of requests.

If you don't care about scalability, then it doesn't matter much which of the two options are you going to choose. But it's probably still better to use the async version, to future-proof your application, so that your application behaves well when scalability becomes an issue.

svick
  • 236,525
  • 50
  • 385
  • 514