Zum Inhalt springen

Dev Tunnels integration

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Dev Tunnels logo

Dev tunnels allow developers to securely share local web services across the internet. Enabling you to connect your local development environment with cloud services, share work in progress with colleagues or aid in building webhooks. Dev tunnels is for adhoc testing and development, not for production workloads.

Dev tunnels are useful for:

  • Sharing a running local service (for example, a Web API) with teammates, mobile devices, or webhooks
  • Testing incoming callbacks from external SaaS systems (GitHub / Stripe / etc.) without deploying
  • Quickly publishing a temporary, TLS-terminated endpoint during development

Before you create a dev tunnel, you first need to download and install the devtunnel CLI (Command Line Interface) tool that corresponds to your operating system. See the devtunnel CLI installation documentation for more details.

To get started with the Dev Tunnels integration, install the 📦 Aspire.Hosting.DevTunnels NuGet package in the AppHost project:

Aspire CLI — Aspire.Hosting.DevTunnels Paket hinzufügen
aspire add devtunnels

Die Aspire CLI ist interaktiv; das passende Suchergebnis wählen, wenn gefragt:

Aspire CLI — Beispielausgabe
Select an integration to add:
> devtunnels (Aspire.Hosting.DevTunnels)
> Other results listed as selectable options...

In the AppHost project, add a dev tunnel and configure it to expose specific resources:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var web = builder.AddProject<Projects.Web>("web");
var tunnel = builder.AddDevTunnel("my-tunnel")
.WithReference(web);

When you run the AppHost, the dev tunnel is created to expose the web application endpoints publicly. The tunnel URLs are shown in the Aspire dashboard. By default, the tunnel requires authentication and is available only to the user who created it.

To allow anonymous (public) access to the entire tunnel, chain a call to the WithAnonymousAccess method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var web = builder.AddProject<Projects.Web>("web");
var tunnel = builder.AddDevTunnel("public-api")
.WithReference(web)
.WithAnonymousAccess();

To configure other options for the dev tunnel, provide the DevTunnelOptions to the AddDevTunnel method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var web = builder.AddProject<Projects.Web>("web");
var options = new DevTunnelOptions
{
TunnelId = "my-tunnel-id",
Description = "QA environment tunnel",
Labels = new[] { "qa", "testing" },
AllowAnonymous = false
};
var tunnel = builder.AddDevTunnel("qa", options)
.WithReference(web);

To allow anonymous access to specific endpoints, use the appropriate WithReference overload:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var api = builder.AddProject<Projects.Api>("api");
var tunnel = builder.AddDevTunnel("mixed-access")
.WithReference(api, "public", allowAnonymous: true)
.WithReference(api, "admin", allowAnonymous: false);

The preceding code exposes:

  • The public endpoint of the api project with anonymous access
  • The admin endpoint of the api project that requires authentication

When another resource references a dev tunnel, environment variables are injected using the Aspire service discovery configuration format:

WEB_HTTPS=https://myweb-1234.westeurope.devtunnels.ms/
services__web__https__0=https://myweb-1234.westeurope.devtunnels.ms/

This lets downstream resources use the tunneled address exactly like any other Aspire service discovery entry.

The DevTunnelOptions class provides several configuration options:

PropertyDescription
DescriptionA description for the tunnel that appears in the dev tunnels service
LabelsA list of labels to apply to the tunnel for organization and filtering
AllowAnonymousWhether to allow anonymous access to the entire tunnel

The DevTunnelPortOptions class provides configuration for individual tunnel ports:

PropertyDescription
ProtocolThe protocol to use (http, https, or auto). If not specified, uses the endpoint’s scheme
DescriptionA description for this specific port
LabelsLabels to apply to this port
AllowAnonymousWhether to allow anonymous access to this specific port
  • Prefer authenticated tunnels during normal development
  • Only enable anonymous access for endpoints that are safe to expose publicly
  • Treat public tunnel URLs as temporary & untrusted (rate limit / validate input server-side)

Dev tunnels automatically:

  • Install the devtunnel CLI if not already available
  • Ensure the user is logged in to the dev tunnels service
  • Create and manage tunnel lifecycle
  • Clean up unmodeled ports from previous runs
  • Provide detailed logging and diagnostics

Tunnels will expire after not being hosted for 30 days by default, so they won’t be forcibly deleted when the resource or AppHost is stopped.

If you see authentication errors, ensure you’re logged in to the dev tunnels service:

Terminal window
devtunnel user login

If you encounter port binding issues, check that no other processes are using the same ports, or configure different ports for your endpoints.

Verify that:

  • The tunnel is running and healthy in the Aspire dashboard
  • You’re using the correct tunnel URL
  • Anonymous access is configured correctly if accessing without authentication
Fragen & AntwortenZusammenarbeitenCommunityDiskutierenAnsehen