1

I want to initiate some method at the moment the code is hosted on the IIS. So far I tried the same by calling that method from global.asax application_start method. But then i have to open the api on the browser once.

For e.g.:- I have a method which creates sql dependency and I want it to be activated just on the moment of deployment on the IIS.Right now I have to browse the api once to make sql dependency activated.

I am using framework 4.5 and MVC Web API

I don't want to use window service or any console solution

Please help me out!!!

nik_boyz
  • 165
  • 3
  • 15
  • can you pls be a little more detail? – Darem Jul 18 '18 at 13:31
  • Possible duplicate of [How to properly autostart an asp.net application in IIS10](https://stackoverflow.com/questions/34634659/how-to-properly-autostart-an-asp-net-application-in-iis10) – DavidG Jul 18 '18 at 13:31
  • If you want to run some code when you deploy your application, then you probably should make it part of your deployment process. – mason Jul 18 '18 at 14:26

1 Answers1

1

most of services are designed stateless or lazy loading to decrease memory consume, so many code will only run when it's needed. with different versions of ASP.NET and MVCs, there are different code to do pre-warm. could you please detail what version it is? what pattern it is, forms or mvc? 5 or core?

here is an official example for ASP.NET forms: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/caching-data/caching-data-at-application-startup-cs

Pre-load your IIS-hosted old-version-.NET application by System.Web.Hosting.IProcessHostPreloadClient here is sample: https://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series

For MVC core,

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .YourFunction(args) //< == add an extension to IWebHostBuilder do your work
            .UseStartup<Startup>()
            .Build();
}

here you can learn more: https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0

Dongdong
  • 2,208
  • 19
  • 28
  • Just follow ScottGu's blog I referenced, it will work: "IIS will start the application in a state during which it will not accept requests until your "warming up" logic has completed. " `IProcessHostPreloadClient ` is right what you are looking for. – Dongdong Jul 18 '18 at 17:39