Error when using config file for Interception in Unity.

Aug 8, 2013 at 10:19 PM
Edited Aug 8, 2013 at 10:56 PM
Hi guys,
I am a starter for Unity Framework, and currently I'm working on using interceptors along with log4net/nlog in Unity Framework.
I am trying to register the whole container and interceptor outside the main app using a config file (for now, it's App.config). However, an error of

"The type name or alias IPerson could not be resolved. Please check your configuration file and verify this type name."

keeps occuring.

And here is my code and config file.
IPerson.cs
using System;

namespace UnityLogging
{
        public interface IPerson
    {
        string Name { get; set; }
        int Age { get; set; }

        void Speak();

        void Run(int speed);
        string Sing(String lyric, int times);
        int CreateException(int [] array, int index);

    }

}
Main program
    
    class Program
    {
        static void Main(string[] args)
        {
           
            IUnityContainer container = new UnityContainer();
            //container.AddNewExtension<Interception>();
            //container.RegisterType<UnityLogging.IPerson, Student>(
            //          new Interceptor<InterfaceInterceptor>(),
            //          new InterceptionBehavior<UnityLogging.NLogInterception>());
            //UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            var section = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
            section.Containers["container"].Configure(container);
            var StudentA = container.Resolve<UnityLogging.IPerson>();
           
            StudentA.Run(100);
            StudentA.Run(12);
            StudentA.Speak();
            Console.WriteLine(StudentA.Sing("do-re-mi",2));
            Console.WriteLine(StudentA.Sing("Hotel California", 100));
            int[] array = new int[5];
            int index = -1;
            Console.WriteLine(StudentA.CreateException(array, index));
            Console.ReadLine();
        }
    }
}
App.config
<?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" />
    <assembly  name="UnityLogging"/>
    
    <alias alias="IPerson" type="UnityLogging.IPerson, UnityLogging"/>
    <alias alias="Student" type="UnityLogging.Student, UnityLogging"/>
    <alias alias="NLoggingInterceptor" type="UnityLogging.NLoggingInterceptor, UnityLogging"/>
    
    <container name="container">
      <extension type="Interception" />
      <register type="IPerson" mapTo="Student">
        <interceptor type="InterfaceInterceptor" />
        <interceptionBehavior type="NLogInterception"/>
      </register>
    </container>

  </unity>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
The error is "The type name or alias IPerson could not be resolved. Please check your configuration file and verify this type name."

I checked my program and app.config many times and still cannot figure out why...
Would anyone help me out?
Thanks!
T.Wu
Editor
Aug 9, 2013 at 12:30 AM
There is a typo in the posted config. There is an alias defined as NLoggingInterceptor but then in the interception config NLogInterception is used.

Also, you should use the LoadConfiguration method:
container.LoadConfiguration("container");
instead of the obsolete Configure method.

Other than that it looks OK to me.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Aug 9, 2013 at 5:12 PM
Thanks for the tip!
However I did what you said. and still the same error pops out ("The type name or alias IPerson could not be resolved. Please check your configuration file and verify this type name.") This error is located on the line of
container.LoadConfiguration("container");
it used to be on the line of:
section.Containers["container"].Configure(container);
Now the configuration part is:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">


    <sectionExtension
      type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,  
             Microsoft.Practices.Unity.Interception.Configuration" />
    <assembly  name="UnityLogging"/>
    
    <alias alias="IPerson" type="UnityLogging.IPerson, UnityLogging"/>
    <alias alias="Student" type="UnityLogging.Student, UnityLogging"/>
    <alias alias="NLogInterception" type="UnityLogging.NLogInterception, UnityLogging"/>
    
    <container name="container">
      <extension type="Interception" />
      <register type="IPerson" mapTo="Student">
        <interceptor type="InterfaceInterceptor" />
        <interceptionBehavior type="NLogInterception"/>
      </register>
    </container>

  </unity>
Editor
Aug 9, 2013 at 5:19 PM
Edited Aug 9, 2013 at 5:20 PM
The posted code and config looks OK so there is something else not quite right. Can you upload a sample project? It should make it easy to see what the issue is.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Aug 9, 2013 at 6:24 PM
That's so kind of you!
But I don't know how to upload project on this site.
Maybe I can send it to some email address?
Thanks!
Editor
Aug 9, 2013 at 6:27 PM
Edited Aug 9, 2013 at 6:28 PM
You can zip it and post it anywhere (e.g. SkyDrive) or email to entlib.support@live.com.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Aug 12, 2013 at 5:12 AM
Thanks.
I have sent the project to entlib.support@live.com with a subject called Thanks to randylevy (Unity config problem)
Thank you again.
Editor
Aug 12, 2013 at 6:52 AM
The reason why the types are not being resolved is because the assembly name of the project is not named UnityLogging. UnityLogging is the namespace but the assembly is actually named unityTest1.

So, the config file could look like this (or you could change the assembly name):
<?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" />
    <assembly  name="unityTest1"/>
    <namespace name="UnityLogging"/>

    <container name="container">
      <extension type="Interception" />
      <register type="IPerson" mapTo="Student">
        <interceptor type="InterfaceInterceptor" />
        <interceptionBehavior type="NLogInterception"/>
      </register>
    </container>

  </unity>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
Instead of using aliases, I just changed the config to use the assembly and namespace elements to perform a type search.

Hopefully that should get you going.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Aug 12, 2013 at 5:02 PM
ohh, yes, that is the problem.
Thank you sooooo much.