Skip to content

Conversation

mitchdenny
Copy link
Member

@mitchdenny mitchdenny commented Apr 29, 2025

Fixes: #8852

Modifies the behavior of DistributedApplicationRunner so that it throws an exception. When used via the Aspire CLI this results in the exception being printed (and the progress indicator showing that it blew up).

image

When the publisher is executed via dotnet the exit code is set (default .NET behavior when an exception bubbles out):

@mitchdenny ➜ .../aspire/playground/publishers/Publishers.AppHost (mitchdenny/make-failing-publisher-exit-non-zero) $ dotnet run -- --publisher explodingpublisher --output-path foo
Using launch settings from /workspaces/aspire/playground/publishers/Publishers.AppHost/Properties/launchSettings.json...
Building...
info: Aspire.Hosting.DistributedApplication[0]
      Aspire version: 9.3.0-dev
fail: Aspire.Hosting.DistributedApplicationRunner[0]
      Failed to publish the distributed application.
      System.NotImplementedException: Boom!
         at ExplodingPublisher.PublishAsync(DistributedApplicationModel model, CancellationToken cancellationToken) in /workspaces/aspire/playground/publishers/Publishers.AppHost/Program.cs:line 83
         at Aspire.Hosting.DistributedApplicationRunner.ExecuteAsync(CancellationToken stoppingToken) in /workspaces/aspire/src/Aspire.Hosting/DistributedApplicationRunner.cs:line 46
fail: Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      Aspire.Hosting.DistributedApplicationException: Publishing failed exception message: Boom!
       ---> System.NotImplementedException: Boom!
         at ExplodingPublisher.PublishAsync(DistributedApplicationModel model, CancellationToken cancellationToken) in /workspaces/aspire/playground/publishers/Publishers.AppHost/Program.cs:line 83
         at Aspire.Hosting.DistributedApplicationRunner.ExecuteAsync(CancellationToken stoppingToken) in /workspaces/aspire/src/Aspire.Hosting/DistributedApplicationRunner.cs:line 46
         --- End of inner exception stack trace ---
         at Aspire.Hosting.DistributedApplicationRunner.ExecuteAsync(CancellationToken stoppingToken) in /workspaces/aspire/src/Aspire.Hosting/DistributedApplicationRunner.cs:line 77
         at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
         at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
Unhandled exception. System.AggregateException: One or more errors occurred. (Publishing failed exception message: Boom!)
 ---> Aspire.Hosting.DistributedApplicationException: Publishing failed exception message: Boom!
 ---> System.NotImplementedException: Boom!
   at ExplodingPublisher.PublishAsync(DistributedApplicationModel model, CancellationToken cancellationToken) in /workspaces/aspire/playground/publishers/Publishers.AppHost/Program.cs:line 83
   at Aspire.Hosting.DistributedApplicationRunner.ExecuteAsync(CancellationToken stoppingToken) in /workspaces/aspire/src/Aspire.Hosting/DistributedApplicationRunner.cs:line 46
   --- End of inner exception stack trace ---
   at Aspire.Hosting.DistributedApplicationRunner.ExecuteAsync(CancellationToken stoppingToken) in /workspaces/aspire/src/Aspire.Hosting/DistributedApplicationRunner.cs:line 77
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
   at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Aspire.Hosting.DistributedApplication.RunAsync(CancellationToken cancellationToken) in /workspaces/aspire/src/Aspire.Hosting/DistributedApplication.cs:line 415
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Aspire.Hosting.DistributedApplication.Run() in /workspaces/aspire/src/Aspire.Hosting/DistributedApplication.cs:line 443
   at Program.<Main>$(String[] args) in /workspaces/aspire/playground/publishers/Publishers.AppHost/Program.cs:line 77

This means the exit code is correctly set for folks that are scripting things this way vs. aspire CLI.

@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Apr 29, 2025
@mitchdenny mitchdenny self-assigned this Apr 29, 2025
@mitchdenny mitchdenny added this to the 9.3 milestone Apr 29, 2025
@mitchdenny mitchdenny marked this pull request as ready for review April 29, 2025 13:35
@mitchdenny mitchdenny changed the title Mitchdenny/make-failing-publisher-exit-non-zero Bubble exceptions out of publishers. Apr 29, 2025
@davidfowl davidfowl merged commit 1839a57 into main Apr 29, 2025
175 checks passed
@davidfowl davidfowl deleted the mitchdenny/make-failing-publisher-exit-non-zero branch April 29, 2025 14:20
@github-actions github-actions bot locked and limited conversation to collaborators May 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AppHost exit code does not match publish task success
2 participants