unity extensions using config file

Oct 1, 2010 at 2:39 PM

Can anybody please point the way to a good (comprehensive) resource that demonstrates how to extend the unity container via the web.config.

in particular I'm looking to add exception handling. I'd also like to be able to use a custom exception handler too so an example of that will also be very helpful. once successful, i plan on potentially adding other extensions such as validation,authorization etc.

everywhere i've looked contains partial information, generally using the fluent API syntax.

please see below for my working config file (the app will compile but throws this error)

{"An error occurred creating the configuration section handler for unity: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) (C:\\Projects\\WaltElviserate\\My Documents\\ProjectWeb\\web.config line 40)"}

where line 40 happens to be the start of the unity section.

<?xml version="1.0"?>
<configuration>
<configSections>
   
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
            <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
    </configSections>

  <!--EXCEPTION HANDLING-->
  <exceptionHandling>
    <exceptionPolicies>
      <add name="GenericPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="someMsg" replaceExceptionType="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceErrorsException, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
 
  <!--Unity section-->
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <!--Is the below correct?-->
      <sectionExtension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
     
    <assembly name="ExampleAssembly"/>
    <namespace name="ExampleDomain"/>
   
    <container name="Default">
     
      <extension type="Interception" />

     
      <register type="ITest" mapTo="TestClass" name="Default">
        <lifetime type="singleton" />
        <interceptor type="InterfaceInterceptor"/>
        <policyInjection/>
      </register>
     
      <interception>
        <policy name="ExamplePolicy">
          <matchingRule name="Everything" type="NamespaceMatchingRule">
            <constructor>
              <param name="namespaceName" value="ExampleDomain"></param>
            </constructor>
          </matchingRule>

          <callHandler type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Exception Handling Call Handler">
            <constructor>
              <param name="exceptionPolicy" dependencyName="GeneralPolicy" />
            </constructor>
          </callHandler>
        </policy>
      </interception>
     
    </container>
   
  </unity>

  <system.web>
 
  <!--removed the rest of config-->
 
</configuration>

any help will be greatly appreciated!

Oct 3, 2010 at 5:11 AM

I think the issue you're having is that there are two different kinds of extensions - container extensions and schema extensions.

Container extensions are added to a Unity container instance at runtime, and provide additional capabilities. Interception is a prime example of this. In the configuration file, they're specified by using the <extension> tag inside a <container> tag.

Section extensions are used by the configuration loader to add elements to the configuration section schema. An example would be the InterceptionConfigurationExtension. Section extensions add new tags, default aliases, namespaces, and assemblies to the configuration file's options. They are only used at config load time. In the configuration file, they're added using the <sectionExtension> tag inside the <unity> section.

So the obvious error you've got in your config file is you are trying to add a container extension, EnterpriseLibraryCoreExtension, as a section extension. This obviously won't work. What you need to do is add <extension type="EnterpriseLibraryCoreExtension" /> inside your <container> element.

 

Oct 5, 2010 at 1:22 PM

 

You are indeed correct. Can you suggest a resource that can explains container extentions so I don't litter this forum with Deep Noob questions? I've looked at the MSVS2008 chm in this regard and it only mentions the coummunity web site.

thanks again!