Exercise - Attach the Visual Studio debugger to an App Service web app
At this point, the app is deployed to Azure, but it isn't functioning correctly. The app still works locally, so it's difficult to determine exactly what's causing the problem without further investigation. Visual Studio can easily help you solve this issue by attaching the debugger to the App Service process out on Azure. This exercise allows you to debug the app as though it's running locally.
Note
Before attempting to attach the debugger, always make sure the state of your local code matches what was deployed to Azure. This ensures that the local symbol files and source code line up with the deployed app. In a real app, if you're using Git to manage your project, you'll want to check out the same commit or release that was deployed.
Configure debugging settings
Make sure you complete the following steps in Visual Studio before debugging an app in Azure to ensure success.
First, make sure you've successfully built your project at least one time. A successful build ensures the source code and any necessary compiled files are ready to go. If your application is running locally, make sure to stop the app.
Navigate to Debug > Options from the top Visual Studio menu. Make sure that Enable Just My code is unchecked, then select OK.
Changing this setting allows Visual Studio to debug the optimized code deployed to Azure using the necessary symbol files from your local
bin
folder. Symbol files are used by the debugger as a bridge between compiled, executing code and the source code in Visual Studio, which is why it's important that your local source code matched the deploy app.
Attach the debugger to the App Service
From the main menu at the top of Visual Studio, select Debug > Attach to process to open the corresponding dialog. Using this window, you can connect and attach to different targets. In this case, you'll connect to the App Service instance you created in the previous step.
Select the Connection Type drop-down and choose the Microsoft Azure App Services option.
Select the Find... button next to the Connection Target field to open a dialog that allows you to browse your Azure subscriptions and app services.
Locate and select the
GitHubBrowser123
App Service you created in the previous step and choose OK.The
w3wp.exe
process should appear in the list of available processes to which to connect, which is the main process of the Azure App Service that hosts the deployed application. Select that process, then choose Attach in the bottom right to connect the Visual Studio debugger.In
Index.cshtml.cs
, go to the first line of theOnPost
method, and set a breakpoint in that method by clicking in the left margin (or right-click and choose Breakpoint > Insert breakpoint).The
OnPost
method inside ofIndex.cshtml.cs
handles most of the logic for the app.Optionally, you can also verify that Visual Studio has loaded the symbol files for your debugging session. Navigate to Debug > Windows > Modules to open the modules window. This window should indicate that the symbol files were successfully loaded for the GitHub browser
.dll
file after the Just my code configuration changes you made earlier.
Troubleshoot the bug
Once your symbols have loaded, you can debug the Azure hosted app just like you would locally.
With the breakpoint set in Visual Studio, switch to the app in the browser, enter a value of dotnet in the app search box, and then hit Submit. Visual Studio will hit the breakpoint inside the
OnPost
method. The first time might take a moment to sync. The code will attempt to retrieve theGitHubUrl
value using theIConfiguration
service. By default, the configuration service loads values from theappsettings.json
file in the app.Use the step over button on the Visual Studio debugging controls (or press F10) to move to the line of code that creates the
searchUrl
. Place your mouse cursor over thegithubUrl
variable above it, you'll find that the value is currently null. This code worked fine locally, so why is the value null in Azure?Open the
appsettings.json
file to investigate further. There are a few configuration settings in this file around logging, but noGitHubUrl
value to be found.Open the
appsettings.Development.json
file.When you set up the sample project, you updated the configuration settings in
appsettings.Development.json
. This file contains configurations that will only be applied while running during development, not when deployed to Azure. Forgetting to set configurations for the production version of your hosted application in Azure is a common source of bugs.Copy the
GitHubUrl
key-value pair fromappsettings.Development.json
and paste it into the top levelappsettings.json
file so that the two files match. The new configuration value will travel with it in theappsettings.json
file when the app is deployed to Azure again.The
appsettings.json
file should look something like this.Detach the debugger from the App Service by pressing the Stop button at the top of Visual Studio, just like a local debugging session.
To redeploy the changes you made, right-click the project node in the Solution Explorer and choose Publish again.
On the publishing profile screen, all of the original deployment settings are still in place, so select Publish again to redeploy to Azure.
When the deployment completes, Visual Studio will launch a browser to display the app again. Enter dotnet into the search form again and press enter. A list of repositories will now load correctly.
Congratulations! You successfully solved a bug in your Azure App Service using Visual Studio.