Skip to content

IHostApplicationLifetime StopApplication not respected in AspNetCore.Mvc.Testing #25857

Description

@GeeWee

Describe the bug

I'm trying to run integration tests, with some things that call IHostApplicationLifetime.StopApplication from an IHostedService

In a "real project" this works, but it does not when using the TestServer.
Test case below

To Reproduce

namespace DefaultNamespace
{
    using System.Threading;
    using System.Threading.Tasks;
    using BetterHostedServices.Test;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc.Testing;
    using Microsoft.AspNetCore.TestHost;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Xunit;

    public class Test
    {
// Dummy startup that does nothing
        public class TestStartup
        {
            public void ConfigureServices(IServiceCollection services)
            {
            }

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
            }
        }

// Hosted Service that stops the application
        public class HostedServiceThatShutsDown : IHostedService
        {
            private IHostApplicationLifetime lifetime;

            public HostedServiceThatShutsDown(IHostApplicationLifetime lifetime)
            {
                this.lifetime = lifetime;
            }

            public Task StartAsync(CancellationToken cancellationToken)
            {
                this.lifetime.StopApplication();
                return Task.CompletedTask;
            }

            public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
        }

// WebApplicationFactory with a few modifications so it can stand-alone
        public class TestWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
        {
            protected override void ConfigureWebHost(IWebHostBuilder builder)
            {
                builder.UseSolutionRelativeContentRoot(
                    "./tests/IntegrationUtils"); // Just because I have a custom dir in my project, can disregard
            }

            //from https://stackoverflow.com/questions/61159115/asp-net-core-testing-no-method-public-static-ihostbuilder-createhostbuilders
            protected override IHostBuilder CreateHostBuilder()
            {
                var builder = Host.CreateDefaultBuilder()
                    .ConfigureWebHostDefaults(x =>
                    {
                        x.UseStartup<DummyStartup>();
                    });
                return builder;
            }
        }

        [Fact]
        public void IssueRepro()
        {
            var factory = new TestWebApplicationFactory<TestStartup>()
                .WithWebHostBuilder(b =>
                    b.ConfigureTestServices(services =>
                    {
                        services.AddHostedService<HostedServiceThatShutsDown>();
                    }));

            var client = factory.CreateClient();

            // Works fine, but it shouldn't. The createClient above should error
            client.GetAsync("/");
        }
    }
}

The IHostedService calls StopApplication. I then don't expect the GetAsync call to work then.
The logs look like this:

info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/geewee/programming/BetterHostedServices/tests/IntegrationUtils
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost/  
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 18.5779ms 404 

So it starts off by writing "Application is shutting down" and then proceeds to start the application.

Further technical details

  • ASP.NET Core version: 3.1
  • Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.401
 Commit:    39d17847db

Runtime Environment:
 OS Name:     fedora
 OS Version:  32
 OS Platform: Linux
 RID:         fedora.32-x64
 Base Path:   /usr/share/dotnet/sdk/3.1.401/

Host (useful for support):
  Version: 3.1.7
  Commit:  fcfdef8d6b

.NET Core SDKs installed:
  2.2.402 [/usr/share/dotnet/sdk]
  3.0.103 [/usr/share/dotnet/sdk]
  3.1.401 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    Rider, but I don't think that matters. All of this is run from the Console.

Activity

  1. added
    area-mvc
    and removed on Sep 14, 2020
  2. added
    affected-few
    bug
    severity-minor
    on Oct 7, 2020
  3. quixoticaxis commented on Jan 15, 2021

    @quixoticaxis

    This, and the fact that MVC.Testing inner infrastructure only disposes the host without stopping it first, makes MVC.Testing really hard to use with any service that has lasting effects on infrastructure.

  4. javiercn commented on Jan 20, 2021

    @javiercn
    Member

    This is a dupe of #29348

  5. 4 remaining items

  6. javiercn commented on Jan 20, 2021

    @javiercn
    Member

    @Tratcher I believe this is a potential issue/enhancement in TestServer, do you have any thoughts?

  7. Tratcher commented on Jan 20, 2021

    @Tratcher
    Member

    @GeeWee While I appreciate the minimal repro, what are you really hoping to accomplish with StopApplication? It's not clear why StopApplication is a useful scenario for TestServer to emulate.

  8. quixoticaxis commented on Jan 20, 2021

    @quixoticaxis

    @Tratcher I don't know the reasons of this issue's author, but in our code we often encounter the following pattern:

    public class ServiceA : IHostedService
    {
        public Task StartAsync(CancellationToken token)
        {
            executing = ExecuteAsync();
        }
    
        private Task ExecuteAsync()
        {
            try
            {
                await serviceLogic.LoopAsync();
            }
            catch
            {
                Log("The service A cannot proceed, stopping the application");
                lifetime.StopApplication();
            }
        }
    
        private Task executing;
    }

    This code enables clean shutdown if any of the services fail, because in the real application StopAsync would be invoked on all of the services. It does not work in the tests though, which leads to inconveniencies (I described our scenario in #29348).

  9. plachor commented on Feb 15, 2021

    @plachor

    Similar in case of OutOfMemory handling described in dotnet/runtime#48157 I would like to perform graceful shutdown once issue is detected and cover such negative path in an integration test.

  10. davidfowl commented on Feb 15, 2021

    @davidfowl
    Member

    I guess the best it could do is stop future requests. That seems fine.

  11. jeremy001181 commented on Feb 24, 2021

    @jeremy001181

    We also have a background service does following, would be good have a way to gracefully stop it after test completed (either pass or failed)

    public class ServiceA : BackgroundService
        {
        protected override Task ExecuteAsync(CancellationToken stoppingToken){
                    while (!stoppingToken.IsCancellationRequested)
                    {
                                 await pickupMessageFromQueueAndDoSomethingAboutIt();
                    }
        }
        }
    
  12. Tratcher commented on Feb 24, 2021

    @Tratcher
    Member

    @jeremy001181 that seems like a different ask than above. That would be handled by calling StopAsync on the host. (Not sure how accessible that is from WebApplicationFactory).

  13. ajeckmans commented on Sep 23, 2021

    @ajeckmans

    Just want to add another use case. Our application does not migrate the database (this is deferred onto a different application that is guaranteed to run before any other in kubernetes). However in our tests we do want this to run and we want a separate database for all tests as they run in parallel. To that end we inject a startup filter that is ran before all others. This startup filter ties into IHostApplicationLifetime.ApplicationStopping and IHostApplicationLifetime.ApplicationStopped to delete the database it has just created and migrated.

    Currently we're having to override WebApplicationFactory.Dispose and through reflection get the _host field and call StopAsync on that to trigger this path. An approach mentioned in #29348.

    There is other reasons why we want these IHostApplicationLifetime callbacks to be triggered, but this one, which is solely for the tests itself, was not yet mentioned here.

    I guess the only question left to ask is: Is this something that you guys would like to see implemented? If so, I would be willing to commit some time to make a PR.

  14. GeeWee commented on Sep 23, 2021

    @GeeWee
    Author

    Maybe also a little extra information for when this would be useful.

    I've been developing BetterHostedServices which relies on being able to understand when the application is being shut down to do some of its magic. Currently I have to jump through a few hoops because it's not easily possible to integration-test anything that relies on shutting down the TestServer.

  15. tapizquent commented on Jan 21, 2022

    @tapizquent

    Are there any workarounds for this? It seems that WebApplicationFactory always leave the process running.
    We are working with a .NET 5 app.

  16. quixoticaxis commented on Jan 22, 2022

    @quixoticaxis

    Are there any workarounds for this? It seems that WebApplicationFactory always leave the process running.
    We are working with a .NET 5 app.

    @tapizquent , there was a PR that forces the host to stop on disposing the factory.
    Disposing the factory should do the trick.

    For other cases, you can use the hack I posted in #29348.

  17. added
    area-networking
    and removed on Aug 24, 2023
  18. added theissue type on May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-fewarea-networkingbuginvestigateseverity-minor

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions