Shutting Down Service Hosts

How should I shut down a running service host?

Here's a variety of attempts at answering the same question, with varying levels of sophistication thrown into the mix.

Way Too Simple

-
Always call abort, it's guaranteed to shut things down.

This is the equivalent of shutting your computer down every day by pulling the plug out. It does what it promises but there's considerable damage as a side-effect.

Bit Too Simple

- Call abort if the service host has failed.

Call close if the service host is running.  

The second attempt alleviates some of the collateral damage but has flaws of its own. If the close fails, then we have no way to fall into the error case.

Simple

- Run your code inside of a catch block.

Call close inside the block on the service host.
  • Call abort if an error occurs inside the block.

Advanced

- Run your code inside of a catch block.

Filter out the exceptions from the service host that are recoverable and use them as a signal to restart the host.  
  • Call abort if an error occurs inside the block.
  • If the error is recoverable, then start a new service host afterwards.
  • Run your code inside of another catch block.
  • Call close inside the block on the service host
  • Call abort if an error occurs inside the block.

Next time: Detecting Metadata