Load controller at startup - web Api core

juan maximiliano aguilar abanto 541 Reputation points
2021-07-19T23:37:57.39+00:00

Hi
Can we call a controller after startup in Web Api Core?

I would like to call a method which saves some data in the cache memory after startup.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2021-07-20T00:07:07.003+00:00

    Can we call a controller after startup in Web Api Core? I would like to call a method which saves some data in the cache memory after startup.

    Move the cache code into a service/class that can be called from the startup.cs file and the Web API Core action.

    0 comments No comments

  2. Anonymous
    2021-08-06T09:21:26.037+00:00

    Hi juanmaximilianoaguilarabanto-6444,

    As far as I know, we could run method after asp.net core application started. You could try to use IHostApplicationLifetime's ApplicationStarted method.

    This method will be called after the application started immediately.

    You could inject IHostApplicationLifetime into Configure() method , then write the callback for ApplicationStarted that would be triggered when the application host has fully started.

    More details, you could refer to below example:

    Register httpclient service in Startup.cs ConfigureServices method

        public void ConfigureServices(IServiceCollection services)
        {
    
            services.AddHttpClient();
    
            services.AddControllersWithViews();
    
    
        }
    

    Add lifetime.ApplicationStarted.Register callback in Configure method:

      public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();
    
    
            app.UseRouting();
            app.UseAuthorization();
    
            app.UseEndpoints(endpoints =>
            {               
                endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Default}/{action=Index}/{id?}");             
    
            });
    
            IHttpClientFactory httpClientFactory = app.ApplicationServices.GetService(typeof(IHttpClientFactory)) as IHttpClientFactory;
    
            lifetime.ApplicationStarted.Register(onApplicationStartedAsync(httpClientFactory).Wait);
        }
    
    
    
        private async Task<Action> onApplicationStartedAsync(IHttpClientFactory httpClientFactory)
        {
    
            var httpclient = httpClientFactory.CreateClient();
    
            var httpMessage = new HttpRequestMessage(HttpMethod.Get, "http://localhost:5000/api/values");
    
            var httpresponse = await httpclient.SendAsync(httpMessage);
    
            if (httpresponse.IsSuccessStatusCode)
            {
                string res = await httpresponse.Content.ReadAsStringAsync();
            }
    
            return null;
        }
    

    Result:

    c0lNB.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.