IIS Autostart with Managed Extensibility Framework

IIS 7.5 included new Application Autostart functionality.  With Autostart, IIS Applications can do things like build a cache or perform some other time-intensive process during an Application’s AppPool startup.  I’ve used Autostart to activate long-running Task Parallel LibraryTasks and Timers.  Autostart configuration requires changes to the ApplicationHost configuration, and while  Autostart configuration is not complicated, it’s also not intuitive.  What follows is a recipe that couples Autostart to Managed Extensibility Framework (MEF) making configuration easier and code more maintainable.

Coding Autostart

Coding Autostart is straightforward.  Simply implement the IProcessHostPreloadClient interface contained in the System.Web assembly and coding is done.  Following is an IProcessHostPreloadClient implementation.

public sealed class PreloadWeb : IProcessHostPreloadClient
{
    #region IProcessHostPreloadClient Members
 
    public void Preload(string[] parameters)
    {
        Task.Factory.StartNew(() =>
        {
            //Do something interesting
        }
        ,TaskCreationOptions.LongRunning);
 
    }
 
    #endregion
}
 

IProcessHostPreloadClient contains one method.  The method is invoked when the Application’s AppPool is activated and the Application AppDomain is created.  The interesting implementation detail is what a developer chooses to do in the method.  Following are some AutoStart implementation scenarios.

  • As demonstrated in the example, an application may need to start a Thread, Timer, or Long Running Task.  The Task may poll an external resource like a SQL Server database or periodically refresh a data cache.
  • The Canonical Autostart example is that it executes a time-consuming process.  For example: Web Services must often be responsive.  Running a time-consuming process when an EndPoint is first tickled may not be realistic if, for example, a Web Service has some sort of time-sensitive service level response time agreement. 

Coding Autostart is almost trivial.  In fact, configuring Autostart is the most difficult Autostart implementation task.

Configuring Autostart

Though the IIS GUI Administration tool tries to unify the configuration process, IIS configuration is stored across multiple Configuration files.  When configuring Autostart, for deployment and troubleshooting reasons it’s important to recognize which configuration is being modified.  Most of the Autostart configuration lives in the Applicationhost.config located under C:\Windows\System32\inetsrv\config. 

Following is how Autostart is configured in the GUI Administration.

The first step, highlight the Admin server root and select Configuration Management.

Configuration Editor
Configuration Editor

Code implementing the IProcessHostPreloadClient is called a service Autostart provider.  To make IIS aware of the provider, navigate to the following location. 

AutoStartProviders
AutoStartProviders

Select the location from the drop-down menu.

Once Autostart Providers is selected, add a new Autostart Provider.  The Autostart Provider setup screen will look like the following.

ClassAndAssembly
ClassAndAssembly

Remember the Provider name because it will be used to configure the Provider on the site.  Enter the class and the assembly containing the class.  Notice how the assembly name omits the DLL extension and the class includes the namespace.  When Autostart activates, IIS will look for what is configured here in the application’s bin directory.

Next navigate to "Sites" just below ServiceAutoStartProviders in the following location.

App Host Sites
App Host Sites

This is where Autostart is enabled on the Site.  Make sure IIS is setup with the .NET 4.0 Framework.  Be sure that the site is configured for a .NET 4.0 Application Pool and that the AppPool is set to autoStart.  AutoStart is on the AppPool properties.

AppPool is set to autoStart
AppPool is set to autoStart

Click on the collection and select the site to configure.  The dialog looks something like the following.

Select the site to configure
Select the site to configure

Set the following properties.  Use the Provider name configured earlier in the process.

Configure Site for AutoStart
Configure Site for AutoStart

That covers configuring and developing Autostart.  Managed Extensibility Framework (MEF) cannot eliminate configuration.  MEF can, however, make configuration more consistent and make code more maintainable.  First, though, a quick MEF review.

MEF Overview

MEF includes classes and attributes that support component discovery and extensibility.  MEF allows applications to load components without baking references into the compiled application.  Two simple ideas underlie MEF solutions. 

  • Components providing functionality are called "Exports". 
  • Components "consuming" an "Export" are called "Imports". 

So, for example, a class within an assembly may "export" functionality that another class in another assembly "imports".

Some core MEF components are described below:

  • Import and Export attributes designate the components a developer will consume or provide.
  • A CompositionContainer gathers the exports and matches exports to imports.  CompositionContainers can, for example, instantiate class instances and apply the instance on a class Property.
  • CompositionContainers read from Catalogs.  MEF components can be housed in Directories and Assemblies.  A CompositionContainer works through, for example, a DirectoryCatalog to gather all MEF components in a Directory.

Turning back to Autostart; the final few steps demonstrate how to couple MEF to Autostart and why MEF is important to maintainability.

Enabling MEF

The following is the Autostart IProcessHostPreloadClient implementation using MEF.

public sealed class PreloadWeb : IProcessHostPreloadClient
{
    #region IProcessHostPreloadClient Members
 
    public void Preload(string[] parameters)
    {
        //MEF code
        var catalog = new DirectoryCatalog(
       AppDomain.CurrentDomain.BaseDirectory + "bin");
 
        using (var container = new CompositionContainer(catalog))
        {
            container.ComposeParts(this);
        }
 
    }
 
    #endregion
}
 

IIS application components are contained in the bin folder.  The CompositionContainer gathers the components from the bin folder and applies instances to the PreloadWeb class.  MEF can be implemented a number of different ways. 

One straight-forward approach is to create a common Interface, implement the interface, and then Export it using an MEF attribute.   If a developer has multiple sites with different Autostart needs MEF would enable a single Provider for all sites.  The bin on each site would contain a common Preload assembly containing the interface and the IProcessHostPreloadClient code.

At first glance MEF may appear to be overkill.  However, consider the following benefits.

First consider Separation of concerns.  Implementing IProcessHostPreloadClient requires the System.Web assembly.   The assembly includes ASP.NET specific components.  Including the reference means that, for example, a Core assembly is yoked to ASP.NET specific components.  Separating an application into a Core and layering server specific features on it is an architecture and maintainability best practice. 

While MEF is not required to achieve separation of concerns, consider reusability.  If your organization has even a handful of sites, an administrator will be responsible for configuring each site.  The MEF implementation would eliminate the need to create the serviceAutostartProvider entries.  For example, instead of 10 serviceAutoStartProvider entries for 10 sites only one entry is needed with the MEF implementation.

Conclusion

The new IIS 7.5 Autostart feature enables functionality that would’ve required creative thinking to implement in prior IIS versions.  MEF within Autostart code can separate IIS specific code from the rest of an application achieving a more reusable and maintainable implementation.

Resources

http://www.iis.net/ConfigReference/system.applicationHost/applicationPools

http://blogs.msdn.com/b/momalek/archive/2011/05/12/writing-an-iis-7-5-auto-start-provider.aspx



Related Articles

Comments

  • plus size corsets cheap

    Posted by Fishnetes1017 on 03/29/2013 07:21am

    http://sexylingeriecostumese.webs.com - Sexy Nurse LingerieThis website guarantees international shipment and includes products from sexy lingerie, swimwear down to sexy Halloween costumes and even sexy shoes Furthermore, should you be uncomfortable in the store, try websites that sell lingerie http://SexyChemise.webs.com - Chemise LingerieHowever, this is not the case today anymore Pay attention to material type (cotton, silk, etc http://discountsexylingerie.webs.com - Lingerie on ClearanceWhen looking for lingerie, try to take into account what she likes Let the environment be tasteful, yet celebratory http://cheapspicylingerie.webs.com - Cheap CorsetsBy listing an auction for your services, you will allow others to compete for your business One more suggestion – browse the website in general http://SexyChemise.webs.com - Chemise LingerieIt is the web The feel of the fabric or material greatly contributes to how well a woman would feel wearing the article

    Reply
  • hats wholesale

    Posted by xxds1xx on 03/29/2013 01:51am

    [url=http://cheaphatsmall.webs.com]cheap snapbacks[/url] cheap snapbacks x grnn [url=http://goodsnapbackhatscheap.webs.com]snapback hats cheap[/url] snapback hats cheap t gvbj[url=http://bestbaseballcap.webs.com]hats wholesale[/url] hats wholesale o yilk[url=http://wholesalefittedhat.webs.com]fitted hats wholesale[/url] fitted hats wholesale d avbe[url=http://snapbackswholesalezone.webs.com]snapbacks wholesale[/url] snapbacks wholesale v lcda[url=http://cheapsnapbackshat.webs.com]cheap hats online[/url] cheap hats online h qofi [url=http://snapbackswholesalezone.webs.com]hats wholesale[/url] hats wholesale n vvvr [url=http://cheaphatsmall.webs.com]cheap snapback hats[/url] cheap snapback hats f axxc[url=http://cheapsnapbacksforsalezone.webs.com]cheap snapbacks for sale[/url] cheap snapbacks for sale g xlzp[url=http://snapbackswholesalezone.webs.com]snapbacks wholesale[/url] snapbacks wholesale l wqva[url=http://snapbackswholesalezone.webs.com]snapbacks wholesale[/url] snapbacks wholesale x omyt[url=http://cheapsnapbackshat.webs.com]cheap snapbacks hats[/url] cheap snapbacks hats l vfpo [url=http://cheapsnapbacksforsalezone.webs.com]cheap snapbacks for sale[/url] cheap snapbacks for sale s uqju [url=http://snapbackswholesalezone.webs.com]hats wholesale[/url] hats wholesale o yqei[url=http://cheapsnapbacksforsalezone.webs.com]cheap snapbacks for sale[/url] cheap snapbacks for sale q xcyp[url=http://snapbackhatwholesale.webs.com]wholesale snapback hats[/url] wholesale snapback hats d szqz[url=http://snapbackswholesalezone.webs.com]snapbacks wholesale[/url] snapbacks wholesale e xudd[url=http://bestbaseballcap.webs.com]wholesale baseball caps[/url] wholesale baseball caps n tlvn

    Reply
  • fake ray ban wayfarer

    Posted by igliliImpumpsvr on 03/29/2013 01:10am

    fake ray ban [url=http://fakeguccisunglasses.webs.com]fake ray ban[/url] fake oakleys discount ray ban [url=http://discountsunglassesfinewebs.com]discount ray ban[/url] cheap ray ban fake ray ban wayfarer [url=http://fakeGucciwayfarer.webs.com]fake ray ban wayfarer[/url] oakley sunglasses discount fake oakleys sunglasses [url=http://bestsunglassesshop.webs.com]fake oakleys sunglasses[/url] replica ray ban cheap fake oakley sunglasses [url=http://bestsunglassesshop.webs.com]cheap fake oakley sunglasses[/url] fake oakley sunglasses fake ray ban [url=http://akeoakleysunglasses.webs.com]fake ray ban[/url] sunglasses wholesale sunglasses cheap [url=http://onlineguciisunglass.webs.com]sunglasses cheap[/url] sunglasses wholesale discount oakley sunglasses,,,,,o [url=http://discountsunglassessale.webs.com]discount oakley sunglasses,,,,,o[/url] discount ray ban

    Reply
  • fake ray ban wayfarer

    Posted by cgliliImpumpheg on 03/28/2013 10:48pm

    http://wholesalesunglasseschic.webs.com - sunglasses wholesale cheap ray ban sunglasses http://onlineguciisunglass.webs.com - ray ban sunglasses cheap fake ray ban wayfarer http://sunglasspomoteauthentic.webs.com - oakley sunglasses cheap oakley sunglasses cheap http://guccisunglassescheap.webs.com - cheap ray ban sunglasses sunglasses cheap http://replicaguccisunglasses.webs.com - replica oakleys discount sunglasses

    Reply
  • ray ban wayfarer cheap

    Posted by xgliliImpumpprz on 03/28/2013 10:24pm

    http://sunglasssaleulow.webs.com - cheap oakleys discount oakley sunglasses http://discountsunglassesfinewebs.com - oakley discount fake oakleys http://akeoakleysunglasses.webs.com - fake ray ban sunglasses wholesale designer sunglasses http://akeoakleysunglasses.webs.com - fake oakleys cheap sunglasses,,, http://sunglasssaleulow.webs.com - cheap oakleys fake ray ban sunglasses

    Reply
  • cheap oakleys

    Posted by ggliliImpumpvnp on 03/28/2013 07:46pm

    http://discountsunglasseshoo.webs.com - discount ray ban sunglasses oakley sunglasses cheap http://sunglasswholesaleofgucci.webs.com - cheap sunglasses online oakley discount http://akeoakleysunglasses.webs.com - fake oakleys cheap ray ban http://fakeguccisunglasses.webs.com - fake oakleys oakley discount http://guccisunglassescheap.webs.com - cheap ray ban oakleys for cheap

    Reply
  • cheap snapbacks free shipping

    Posted by xxds4ny on 03/28/2013 10:07am

    [url=http://wholesalefittedhat.webs.com]snapbacks wholesale[/url] snapbacks wholesale q zxck [url=http://snapbackswholesalezone.webs.com]snapback hats wholesale[/url] snapback hats wholesale a lavt[url=http://wholesalefittedhat.webs.com]wholesale fitted hats[/url] wholesale fitted hats g nhai[url=http://wholesalefittedhat.webs.com]snapback wholesale[/url] snapback wholesale f cspn[url=http://snapbackhatwholesale.webs.com]wholesale fitted hats[/url] wholesale fitted hats l pjtj[url=http://snapbackswholesalezone.webs.com]fitted hats wholesale[/url] fitted hats wholesale n yuuw [url=http://cheapsnapbacksforsalezone.webs.com]cheap snapbacks free shipping[/url] cheap snapbacks free shipping l yjlr [url=http://goodsnapbackhatscheap.webs.com]cheap snapback hats[/url] cheap snapback hats t rokp[url=http://cheapsnapbackshat.webs.com]cheap hats[/url] cheap hats n aelr[url=http://cheapsnapbacksforsalezone.webs.com]snapback hats cheap[/url] snapback hats cheap r yjxk[url=http://goodsnapbackhatscheap.webs.com]snapback hats cheap[/url] snapback hats cheap w tczg[url=http://snapbackswholesalezone.webs.com]fitted hats wholesale[/url] fitted hats wholesale t lxow [url=http://snapbackhatwholesale.webs.com]snapback hats wholesale[/url] snapback hats wholesale t zhbw [url=http://goodsnapbackhatscheap.webs.com]cheap snapbacks free shipping[/url] cheap snapbacks free shipping o mcjh[url=http://bestbaseballcap.webs.com]wholesale snapback caps[/url] wholesale snapback caps q yguz[url=http://bestbaseballcap.webs.com]wholesale snapback caps[/url] wholesale snapback caps g eafu[url=http://wholesalefittedhat.webs.com]wholesale fitted hats[/url] wholesale fitted hats o rach[url=http://goodsnapbackhatscheap.webs.com]cheap snapbacks free shipping[/url] cheap snapbacks free shipping h yoly

    Reply
  • ugg boots xqjuvu

    Posted by Suttoncor on 02/20/2013 01:14pm

    cheap ugg boots sale qldyfnen cheap ugg boots uk bsefyqwc cheap ugg boots kcjcaiwf cheap uggs sgpfgbah ugg boots sale uk ckveulpf ugg boots sale tjwihtwq ugg boots uk inllmyde ugg boots lukixjvp ugg sale yygwbqcl

    Reply
  • ghd australia cnymvy

    Posted by Mandyusv on 02/11/2013 08:15pm

    6zEfs christian louboutin lHkg longchamp outlet wHxv michael kors outlet 6gUhe 7dTja chi 6vJvs michael kors outlet 1iCpn cheap nfl jerseys 1mGoj nike uk 1yVwv ghd 5wSki ugg 9dVef toms outlet 1lUey Tory Burch Red Suede High Heel Boots Cheap 3nUbd hollister pas cher 5sCfx ghd 1qCfb cheap uggs

    Reply
  • ghd australia fwtqmr

    Posted by Suttonwvr on 02/07/2013 12:56am

    5uWsr christian louboutin uBlq longchamp outlet fMiz michael kors outlet 3oOwx 9qTqh chi 9mWwu michael kors outlet 8mCwz cheap nfl jerseys 9hXju nike uk 0vTro ghd 8xCeg ugg 5tKnk toms outlet 3eQmk Tory Burch Wedges 7iSwh hollister 3dNsk ghd 1xAdi cheap ugg boots

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Ever-increasing workloads and the challenge of containing costs leave companies conflicted by the need for increased processing capacity while limiting physical expansion. Migration to HP's new generation of increased-density rack-and-blade servers can address growing demands for compute capacity while reducing costly sprawl. Sponsored by: HP and Intel® Xeon® processors Intel, the Intel logo, and Xeon Inside are trademarks of Intel Corporation in the U.S. and/or other countries. HP is the sponsor …

  • Featuring CLC bio and IBM Research Event Date: March 19, 2014 High-performance computing (HPC) and storage solutions developed for the technical community are required to process the data produced by Next Generation Sequencing that is doubling every five months. (Baker M., Nature Methods 7, 2010, 495). In the four phases in a sequencing project: (a) Experimental design and sample collection, (b) Sequencing (c) Data management and (d). Downstream analysis, it takes a highly optimized HPC platform to keep …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds