简体   繁体   中英

asp.net core: What is the best practice to execute code in a container application for kubernetes

I created a container application for kube.netes project pattern. I want to run there a service that listens to messages and do work in an endless loop. I execute it from Configure method Startup.cs . From some references this is the place to do so. Before that i register all services to ConfigureServices 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 Configure method

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            var cancellationToken = new CancellationTokenSource();
            var tasksListener = app.ApplicationServices.GetRequiredService<ITaskListener>();
            var appLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
            appLifetime.ApplicationStopping.Register(() =>
            {
                Console.WriteLine("ApplicationStopping is stopping");
            });
            tasksListener.Listen(cancellationToken.Token).Wait();
            app.Run(async (context) =>
            {
                await tasksListener.Listen(cancellationToken.Token);
            });
        }

I'm able to stop the application and see the print, but the code inside app.Run isn't executed. If I switch this code:

            app.Run(async (context) =>
            {
                await tasksListener.Listen(cancellationToken.Token);
            });

with this:

                tasksListener.Listen(cancellationToken.Token).Wait();

so the code is executed but then IApplicationLifetime.ApplicationStopping won't be executed. Any idea?

for those who encounter same problem I'll update that the solution for me was simply delete Startup.cs and do everything from Program.cs , so it will look like this

    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) =>
            WebHost.CreateDefaultBuilder(args)
            .Configure((app) =>
            {
                var appLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
                appLifetime.GracefullyTerminate(worker, cancellationToken);
            });
    }

what i found out is that Startup.cs fit to implement app service that get calls from outside. app.Run refer to what will happen when a request will come.

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 https://learn.microsoft.com/en-us/as.net/core/fundamentals/host/hosted-services?view=as.netcore-3.1&tabs=visual-studio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM