Troubleshooting 502 Responses in Azure App Services
I recently deployed code that worked on my local machine to an Azure App Service and started getting 502 - Bad Gateway
responses. Worst of all, I wasn't seeing any helpful warnings or error messages come through in Application Insights; not even my own custom log statements that I was 100% sure should have been appearing because they were called well before the custom code that I pushed up. In this post I will discuss how I troubleshot this issue and show that App Services don't support all libraries.
Speedrunning the Troubleshooting Process
Knowing that log statements weren't working as expected, I opted for adding early return
statements in the code to isolate the issue.
When encountering issues that aren't easy to troubleshoot, the first thing I do is reduce my dev cycle time. That is, reduce the amount of time that it takes to make a change, build, deploy, and test.
In this case, my local dev cycles were already short, but I needed to reduce the time it took to deploy to the upstream App Service. I skipped the build pipeline entirely and just dragged and dropped the updated DLLs into the bin folder of the App Service. App Services make this easy because Kudu is built in, and it's trivial to drag and drop files into the App Service file system (FTP is dead; long live FTP!).
The Offending Code
I isolated the issue to the hunk of code below. Very few developers would ever need to use this library, but the point is that Azure App Services don't support all libraries.
Takeaways
There are some libraries that Azure App Services don't support. When you deploy code that use such libraries, you may get 502 responses. Log statements may not work in those cases. To troubleshoot, reduce your dev cycle time and isolate the issue. In my case, the offending code was using System.Windows.Forms
. This appears to be because Windows Forms relies on a GUI which requires a Windows environment with a user session. That explains why it was working locally, but not on App Services, which are intended to host web applications only (source).
Keep going,
-MG