Using Enterprise Library Exception Handling in ASP .NET 2.0 with Visual Studio 2005

Everyone trying to use Enterprise Library Exception Handling in Visual Studio 2005 website project surely encountered a problem like this:

Visual Studio 2005 error

This happens when entlib is enabled in web.config and you are trying to run a website in debug mode. Visual studio parses web.config to see wheather debugging is enabled and encounters entlib schema with no definition and throws an exception.

Some people proposed attaching VS to a running process in order to debug a website, but that was not really an option for me. So I came up with another solution. My solution takes advantage of web deployment project and some custom scripting with MSBuild.

First of all your website has to contain 2 .config files, for example: Web.config and Release.config. Web.config should NOT contain EntLib configuration, in Release.config put everything from web.config plus entlib configuriation. Essentialy Web.config will be used in Debug and Release.config will be used in deployed website.

<?xml version=1.0?>

<!– Web.config –>

<configuration

  <appSettings/>

  <connectionStrings/>

  <system.web>

    <compilation debug=true>

    </compilation>

    <authentication mode=Windows/>

    <!–

        <customErrors mode=”RemoteOnly” defaultRedirect=”GenericErrorPage.htm”>

            <error statusCode=”403″ redirect=”NoAccess.htm” />

            <error statusCode=”404″ redirect=”FileNotFound.htm” />

        </customErrors>

        –>

  </system.web>

</configuration>

<?xml version=1.0?>

<!– Release.config –>

<configuration>

  <configSections>

    <section name=enterpriselibrary.configurationSettings type=System.Configuration.IgnoreSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 />

  </configSections>

  <appSettings/>

  <connectionStrings/>

  <system.web>

    <compilation debug=true>

    </compilation>

    <authentication mode=Windows/>

  </system.web>

  <enterpriselibrary.configurationSettings xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema applicationName=Web.config xmlns=http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration>

    <configurationSections>

      <configurationSection name=exceptionHandlingConfiguration encrypt=false>

        <storageProvider xsi:type=XmlFileStorageProviderData name=XML File Storage Provider path=exceptionHandlingConfiguration.config />

        <dataTransformer xsi:type=XmlSerializerTransformerData name=Xml Serializer Transformer>

          <includeTypes>

            <includeType name=LoggingExceptionHandlerData type=Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.Configuration.LoggingExceptionHandlerData, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null />

          </includeTypes>

        </dataTransformer>

      </configurationSection>

      <configurationSection name=loggingConfiguration encrypt=false>

        <storageProvider xsi:type=XmlFileStorageProviderData name=XML File Storage Provider path=loggingConfiguration.config />

        <dataTransformer xsi:type=XmlSerializerTransformerData name=Xml Serializer Transformer>

          <includeTypes />

        </dataTransformer>

      </configurationSection>

      <configurationSection name=loggingDistributorConfiguration encrypt=false>

        <storageProvider xsi:type=XmlFileStorageProviderData name=XML File Storage Provider path=loggingDistributorConfiguration.config />

        <dataTransformer xsi:type=XmlSerializerTransformerData name=Xml Serializer Transformer>

          <includeTypes />

        </dataTransformer>

      </configurationSection>

    </configurationSections>

    <keyAlgorithmStorageProvider xsi:nil=true />

    <includeTypes />

  </enterpriselibrary.configurationSettings>

</configuration>

Now you have to add a web deployment project and open it in editor using Open Project File from web deployment context menu. Uncomment and edit AfterBuild Target.It will replace web.config with release.config right after build.

  <Target Name=AfterBuild>

    <Copy Condition= ‘$(Configuration)’ == ‘Release’ SourceFiles=$(WDTargetDir)\Release.config DestinationFiles=$(WDTargetDir)\Web.Config />

    <Delete Files=$(WDTargetDir)\Release.config />

  </Target>

Of course it will only work when you are using entlib only for exception handling. And it makes only sense when you don’t need entlib while debugging, but that should be true for most of the people using only exception handling.