Fastest way to restart a .NET web app programmatically

1.1k Views Asked by At

I’m writing some e2e tests for a web application where there is a lot of code I need to interact with that is outside my control. In order to setup my application at the start of each test or group of tests, I intend to use sql scripts to seed the database into a known state and then force the web app to restart so that all internal caches outside my control are all reset.

With that in mind, does anyone know what approach will give me the fastest restart of a web app?

Options I’ve got are:

  1. Touch the web.config
  2. Create then remove an app_offline.htm file in the root
  3. Create some API that can call HttpRuntime.UnloadAppDomain

Are any of these methods faster than any other? Are are they all effectively the same?

3

There are 3 best solutions below

2
On

Use option # 3:

System.Web.HttpRuntime.UnloadAppDomain();

This method terminates the current application. The application restarts the next time a request is received for it.

See: https://learn.microsoft.com/en-us/dotnet/api/system.web.httpruntime.unloadappdomain?view=netframework-4.8

1
On

I Suggest, expose an endpoint which will clear/refresh the cache. Can that endpoint from your e/e test.

2
On

As per your suggested methods:

  1. Touch the web.config
  2. Create then remove an app_offline.htm file in the root
  3. Create some API that can call HttpRuntime.UnloadAppDomain

Both 1 and 2 seem a little clunky and require coding a separate API to do programmatically. 3 Seems less clunky but definitely requires coding an API.

As per my comments I am not sure that restarting the app is necessary or will accomplish what you need, as the data being reseeded will not be cached in the GAC. If the data is coming from a Web API then you might consider restarting that API. The best way to do that is through the [![IIS AppPool advanced settings][1]][1]:

You want to make sure you settings are as follows: Start Mode: OnDemand *Idle Time-out (Minutes): 0 Idle Time-out Action: Terminate

All those settings are default except for the time-out minutes, which are 20 by default. By setting it to 0 on the your app or the third part AP the application will Terminate (dumped from the GAC) and will restart upon a new web request.

enter image description here