Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating AppDomain recycle

Tags:

c#

asp.net

I read Phil Haack's article on using the IRegisteredObject interface to be sure that a background thread finishes completion when the AppDomain is brought down, and I implemented it into my code. However, I'm not entirely sure how to simulate a AppDomain crash in order to test it. How can I simulate an AppDomain crash?

like image 348
hemlocker Avatar asked Jul 10 '12 15:07

hemlocker


1 Answers

Easiest way to get an AppDomain to shutdown is by causing the parent process to stop. In ASP.NET you can do this by recycling or stopping the application pool that your code is running under. We have some code that does that to make sure logging gets flushed and that's how I've tested it in the past.

As for a complete crash, I'm not really sure how you could go about that... The hosting environment catches exceptions so it might not be doable. You could try Environment.Exit or Environment.FailFast which will force the parent process to exit immediately?

Edit: I forgot the easiest option here... Just save Web.config - this will trigger an AppDomain recycle!

Some alternates...

If you're running using IIS you can recycle the application pool starting IIS Manager and following these steps...

  1. Locate your application beneath 'Sites'. It maybe beneath one of the websites in there - possibly as a virtual directory.
  2. Right-click the application and select 'Manage Application' then 'Advanced Settings'.
  3. Make a note of the 'Application Pool'.
  4. Locate the application pool beneath 'Application Pools'.
  5. Right-click it and hit recycle.

For IIS Express you can simply right-click the icon in the tray and in the UI you should be able to stop the application there.

For Visual Studio Development Server (Cassini) you can also use the tray to stop the application.

like image 180
Dean Ward Avatar answered Oct 04 '22 00:10

Dean Ward