<p>I created a container application for kubernetes project pattern. I want to run there a service that listens to messages and do work in an endless loop. I execute it from <code>Configure</code> method <code>Startup.cs</code>. From some references this is the place to do so. Before that i register all services to <code>ConfigureServices</code> method like a normal asp.net app.
Also, I want to use IApplicationLifetime.ApplicationStopping to be triggered.
I didn't find a way to achieve both things: execute the code of the task listener and controlling the callback of ApplicationStopping.
this is my <code>Configure</code> method</p>
<pre><code>        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            var cancellationToken = new CancellationTokenSource();
            var tasksListener = app.ApplicationServices.GetRequiredService&lt;ITaskListener&gt;();
            var appLifetime = app.ApplicationServices.GetRequiredService&lt;IApplicationLifetime&gt;();
            appLifetime.ApplicationStopping.Register(() =&gt;
            {
                Console.WriteLine(&quot;ApplicationStopping is stopping&quot;);
            });
            tasksListener.Listen(cancellationToken.Token).Wait();
            app.Run(async (context) =&gt;
            {
                await tasksListener.Listen(cancellationToken.Token);
            });
        }
</code></pre>
<p>I'm able to stop the application and see the print, but the code inside <code>app.Run</code> isn't executed.
If I switch this code:</p>
<pre><code>            app.Run(async (context) =&gt;
            {
                await tasksListener.Listen(cancellationToken.Token);
            });
</code></pre>
<p>with this:</p>
<pre><code>                tasksListener.Listen(cancellationToken.Token).Wait();
</code></pre>
<p>so the code is executed but then IApplicationLifetime.ApplicationStopping won't be executed.
Any idea?</p>

lior.mor
<p>for those who encounter same problem I'll update that the solution for me was simply delete <code>Startup.cs</code> and do everything from <code>Program.cs</code>, so it will look like this</p>
<pre><code>    public class Program
    {
        public static void Main(string[] args)
        {
            IWorker worker = new Worker();
            CancellationTokenSource cancellationToken = new CancellationTokenSource();
            CreateWebHostBuilder(args, worker, cancellationToken).Build().RunAsync();
            worker.Work(cancellationToken.Token).Wait();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args, IWorker worker, CancellationTokenSource cancellationToken) =&gt;
            WebHost.CreateDefaultBuilder(args)
            .Configure((app) =&gt;
            {
                var appLifetime = app.ApplicationServices.GetRequiredService&lt;IApplicationLifetime&gt;();
                appLifetime.GracefullyTerminate(worker, cancellationToken);
            });
    }
</code></pre>
<p>what i found out is that <code>Startup.cs</code> fit to implement app service that get calls from outside. app.Run refer to what will happen when a request will come.</p>
<p>In addition, microsoft launched in .net core 3.0 a Worker Service project template that fit exactly to this purpose. you can find reference here <a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&amp;tabs=visual-studio" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&amp;tabs=visual-studio</a></p>
