Environment: ASP.NET MVC 5, SQL Server
Here is my method that returns current user's profile from the database:
public static ProfileModel getCurrentProfile(HttpContextBase ctx) {
User user = AccountController.getUser(ctx);
Task<ProfileModel> model = ProfileModel.getValue(user.UserID);
model.Wait();
return model.Result;
}
Upon execution, model.Wait()
just hangs up.
I have read several articles about deadlocks and using ConfigAwait(false)
for such situations. However, there would be lot of places I would need to call this method. I am thinking if I fix it the right way, I may be able to avoid ConfigAwait()
calls altogether.
Here is how I am using the method in my index.cshtml
file:
Members.Models.ProfileModel userModel = HomeController.getCurrentProfile(Context);
Html.RenderPartial("_PartialPublicProfile", userModel);
File _PartialPublicProfile
requires ProfileModel
instance to be passed in. Is it possible to pass in Task<ProfileModel>
instance as a parameter?
Or, is there a better way to solve the problem? Regards.
You're essentially trying to run an async task synchronously. You have to be very careful about how you do that or else your application can and will hang.
In Entity Framework 6, there are now sync and async data access methods, but importantly, the sync versions call the async versions synchronously. To pull this off the EF team uses the following internal helper class. I would recommend implementing this as your own helper class, and then using it in all scenarios where you need to call an asynchronous method synchronously:
So, in your particular scenario here, you code would change to: