Unity 3.0 registration troubles

Jun 17, 2013 at 7:14 PM
Thanks in advance for your help...

I am struggling with Unity 3.0 trying to get types and interception registered correctly. The problem begins with Unity not liking the app.config file. Here is a snippet from a test program:
       try
        {
            container.LoadConfiguration(interceptedName);
        }
        catch (Exception ex)
        {
            Console.Write("Exception:"); // just so we can break and examine the exception
            Console.WriteLine(ex.Message);
            // This should not be necessary in Code!
            container.RegisterType<TestClass>(
                new Interceptor<VirtualMethodInterceptor>(),
                new InterceptionBehavior<TestAndDebugLoggingInterceptor>());
        }
"interceptedName" is equal to "TestAndDebugLoggingInterceptor." It throws an InvalidOperationException:
 The type name or alias 
      TestAndDebugLoggingInterceptor 
 could not be resolved. Please check your configuration file and verify this type name.
The call to container.RegisterType proceeds without an exception. However, when I trace through the code, the interceptor (TestAndDebugLoggingInterceptor) is registered twice, and it indeed calls it twice before calling the target method(s) in TestClass. At first, I thought that perhaps the registration from the app.config file was succeeding despite the exception. But, commenting out the call to container.RegisterType results in an exception that the type cannot be resolved.

I can post the app.config file if anyone wants to see it, but I am guessing at this point that it is not relevant, because of the exception being thrown. Aside from whatever syntax or path error there is in the app.config file, does anyone have any idea why RegisterType ends up wrapping the targets twice? The syntax for that call is straight out of MSDN examples, so I am somewhat stumped at this point.

Thanks again.
Jun 17, 2013 at 8:04 PM
My bad, I said the parameter passed to container.LoadConfiguration was the type being intercepted, but it is actually the container name from the app.config file ("InterceptTestContainer.") It would seem that the code is finding that container name in the app.config file, but doesn't like the assemblies or paths being specified. The relevant section from the app.config file is:
<container name="InterceptTestContainer">
   <extension type="Interception" />
   <register type="Paychex.IP.Common.TestObjects.TestClass, TestObjects" mapTo="Paychex.IP.Common.TestObjects.TestClass, TestObjects">
      <interceptor type="VirtualMethodInterceptor"/>
      <interceptionBehavior type="TestAndDebugLoggingInterceptor" />
   </register>
</container>
Again, the syntax is based on what I have read in MSDN.
Jun 17, 2013 at 8:28 PM
You didn't post the full config so I'll mention that you need to add the sectionExtension:
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />

Also, the type TestAndDebugLoggingInterceptor needs to be qualified unless an alias is created or the assembly and namespace search tags added to the config file.

The configuration should look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <container name="InterceptTestContainer">
      <extension type="Interception" />
      <register type="Paychex.IP.Common.TestObjects.TestClass, TestObjects" mapTo="Paychex.IP.Common.TestObjects.TestClass, TestObjects">
        <interceptor type="VirtualMethodInterceptor"/>
        <interceptionBehavior type="Paychex.IP.Common.TestObjects.TestAndDebugLoggingInterceptor, TestObjects" />
      </register>
    </container>    
  </unity>
</configuration>
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jun 17, 2013 at 8:59 PM
Thanks, Randy, that was it.

Actually, the TestAndDebugLoggingInterceptor is in a different namespace, but the problem was just what you said: it was not fully qualified. I did have the sectionExtension declaration in the app.config, but I missed the qualification on the TestAndDebugLoggingInterceptor. Thanks!