问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/20715175/understanding-async-and-await