Use dev tunnels in Visual Studio to debug your web APIs

November 23, 2022 by Anuraj

VisualStudio Web API Debugging Dev Tunnel

This post is about using dev tunnel in Visual Studio to debug your web APIs. Usually when we build applications which requires a callback from an external service I used to implement a tunneling software like ngrok - one example is implementing power platform connectors. Recently Visual Studio introduced a feature called dev tunnels - here is the blog post about it - this feature helps us to debug web api endpoints with a publicly accessible endpoint.

To use this feature, first we need to enable Dev Tunnel feature from Tools > Options. Then search for keyword dev.

Enable Dev Tunnel feature

Next we need to configure the User Account - I am using my GitHub account.

Select user to use dev tunnels

Now we are ready to enable dev tunnel in our applications. Create a web api project in visual studio. Modify the launchSettings.json file under Properties folder. Add the following code under https section.

"devTunnelEnabled": true,
"devTunnelAccess": "Public"

Now the https section of my launchSettings.json looks like this.

"https": {
  "commandName": "Project",
  "dotnetRunMessages": true,
  "launchBrowser": true,
  "launchUrl": "swagger",
  "applicationUrl": "https://localhost:7113;http://localhost:5263",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "devTunnelEnabled": true,
  "devTunnelAccess": "Public"
}

Now if we run the application, we will be able to see a different URL - not the localhost one like this.

Dev Tunnel First Screen

We will see this screen only one time. Once we click on continue, we will be able to access the application with the URL.

Web API using dev tunnels

We can get the tunnel URL from using the environment variable like this - Environment.GetEnvironmentVariable("VS_TUNNEL_URL"). If we got multiple project, the environment variable will be like this VS_TUNNEL_URL_<Project Name>

Using dev tunnel feature will help us debugging Web APIs over public internet.

Happy Programming.

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub