Debug ASP.NET Core REST API in Azure App Services
This is a quick tutorial on how to launch your backend API in debug mode to azure app services.
To give some context:
After publishing my ASP.NET Core REST API to azure and fixing an initial SQL connection error, I tried doing some testing from Postman and from my locally hosted Angular frontend. What I ran into was a wall of 401 unauthorized errors. I figured at first that it might be a CORS problem, so I configured CORS in Azure to allow all origins. Same error. So everything seemed to be pointing to an invalid login, but what was weird is my DbInitializer class successfully created an admin user in the SQL DB, which I confirmed through doing a query in the Azure portal.
Through publishing my API to azure in debug mode and setting breakpoints, I was able to figure out that the password for the initial admin user was indeed not what the DbInitializer class supposedly set it to. Why that occurred? Honestly I didn’t bother to figure it out (I was ready to have this thing working, haha), but instead did a quick work around by disabling the user authentication, creating a new admin user from within the app, and then re-enabling user authentication and republishing. From that point everything worked as it should.
Here’s how to do the debug mode
First set a break point in your code:
Next we need to publish the API to azure again but in debug mode:
Go ahead and click “Edit” under the select field, and you should get a pop up. Click settings and then select debug mode for “Configuration”:
Save the settings and then hit publish. Once your deployment is finished, you’ll need to attach a debugger to it.
Go to cloud explorer, make sure you’re signed into your azure account, which you should be since you just deployed it…Select the app service you want to debug and hit the “attach debugger” command found in the actions window.
You’re API will launch and you can now try making your HTTP request, trigger the breakpoint, and step through your code.
*Quick Tip: If you want to edit your code for testing, you’ll have to stop the debugger, edit it, and then republish it to azure.
I hope that helps.
Happy Debugging!