Wednesday, September 08, 2010

Host your own Web Server in your application using IIS 7.0 Hostable Web Core

  IIS 7.0 includes a very cool feature that is not so well known called Hostable WebCore (HWC). This feature basically allows you to host the entire IIS functionality within your own process. This gives you the power to implement scenarios where you can customize entirely the functionality that you want "your Web Server" to expose, as well as control the lifetime of it without impacting any other application running on the site. This provides a very nice model for automating tests that need to run inside IIS in a more controlled environment.
  This feature is implemented in a DLL called hwebcore.dll, that exports two simple methods:

  • WebCoreActivate. This method allows you to start the server. It receives three arguments, out of which the most important one is applicationHostConfigPath that allows you to point it to your very own copy of ApplicationHost.config where you can customize the list of modules, the list of handlers and any other settings that you want your "in-proccess-IIS" to use. Just as ApplicationHost.config you can also specify the "root web.config" that you want your server to use, giving you the ability to completely run an isolated copy of IIS.
  • WebCoreShutdown. This method basically stops the server listening.

The real trick for this feature is to know exactly what you want to support and "craft" the IIS Server configuration needed for different workloads and scenarios, for example:
  • Static Files Web Server - Supporting only static file downloads, good for HTML scenarios and other simple sites.
  • Dynamic Web Sites
  • ASPX Pages
  • WCF
  • Custom set of Http Modules and Handlers

  • All of the above



  •   An interesting thing to mention is that the file passed to ApplicationHostConfigPath parameter is live, in the sense that if you change the configuration settings your "in-process-IIS" will pick up the changes and apply them as you would expect to. In fact even web.config's in the site content or folder directories will be live and you'll get the same behavior.
    Read more: IIS