Skip to main content

Async Task .Result or .Wait() not working in MVC web application

I had a piece of async code that worked as a console application in Visual Studio 2015. I had to move this code into my MVC web application.

In the code, basically you pass an ID to an Api and it returns a document corresponding to that ID. The issue was, there needed to be 2 calls to the Api. As part of the first call you pass the ID and the Api returned a filename. You then pass the filename to the second call and it returns the corresponding document.

This worked fine in a console application. But when I tried to port it into a MVC web application it did not work. Frankly async calls work fine in MVC applications, but in instances like mine, I needed the code to be synchronous, since the second call to the Api should only start after the first call returns valid data. I even tried to make the code synchronous, but this just caused my application to hang at the .Result line (code below)




I tried googling around for a while and finally stumbled upon the following article.
https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/

And reaziled that this is a deadlock issue within ASP.NET. So I updated my code as per the above link



And everything worked in my MVC application. Hooray!
Hope this helps someone else facing a similar situation.

Comments

Popular posts from this blog

Sitecore: Get list of logged in users

I had a deployment today and wanted to find a list of users who were logged into the Sitecore admin site. This was mainly so that I can contact them and let them know that a deployment was going to happen. I found the following link very useful as it gave me exactly what I was looking for. A list of users that were logged in and I contacted them. It also has the ability to Kick off users! http://{YourWebsite}/sitecore/client/Applications/LicenseOptions/KickUser Note: You can only see other users in this list if you have the right administrator permission. Logging in with a lower access level user only gave me the logged in user and no one else on the list.

Create Object XML while Debugging in VS

 There are times when you put breakpoints in Visual studio and read object values within Visual Studio. This is all good if you are doing some debugging. But if you want to save an object like a Json object in Visual studio there isn't a straightforward way to do that. I found this piece of code that I copied and pasted in the Immediate Window in Visual Studio and was able to save the object as an XML file. (new System.Xml.Serialization.XmlSerializer(YourObject.GetType())).Serialize(new System.IO.StreamWriter(@"c:\tmp\YourObject.xml"), YourObject) Happy Sitecoreing!

Updating Sitecore Image alt text

One of the most important conditions of making a site accessible is to make sure that all images on the site have the alt field with some value that describes the image. The simplest update we can make to Sitecore is to have the alt field automatically get the image file name. That way even if content authors forget to fill the alt field, it is pre-filled with the file name. To do this just add $name to the alt field in the standard value of an image [/sitecore/templates/System/Media/Unversioned/Image/__Standard Values] This is all good for an future images that gets uploaded to Sitecore. But what about all the existing images. For that we can write a PowerShell script (see below) to get all images in the Sitecore image folder that have empty alt tags. Export that to a csv file. $pathOfImages = "master:/sitecore/media library/MyImages" $images = Get-ChildItem -Path $pathOfImages -Language * -Recurse | Where-Object { ($_.Fields["Alt"] -ne $null) -and ($_.Fields...