Unity Configuration from app.config

Oct 29, 2008 at 9:18 PM
Hi,

I'm learning to use de Unity Application Block, and I've work perfect while I was mapping types from code. Now, I want to configure types from an App.config file. I tooke documentation examples but I've got a TypeLoadException.

This is my App.config:

<configSections>
    <section name="unity"
                 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <unity>
    <containers>
      <container >
        <types>
          <type type="UnityTest.IMyInterface" mapTo="UnityTest.MyClass" />
        </types>
      </container>
    </containers>
  </unity>

From code:

            IUnityContainer myContainer = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(myContainer);

Last line throws the TypeLoadException:

System.TypeLoadException was unhandled
  Message="Could not load type 'UnityTest.IMyInterface' from assembly 'Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."
  Source="mscorlib"
  TypeName="UnityTest.IMyInterface"

But, the name and name space of the interface are right.

      namespace UnityTest
     {
          public interface IMyInterface

So, I have no idea about where is the problem from. I hope someone can give me a guide about it.

Thanks!




Oct 29, 2008 at 10:24 PM
You need to specify the full type names for both the interface and the type. That name includes the assemblies that those types are defined.

For example, when you specified the UnityConfigurationSection, notice there's a lot more there than just the namespace and type name:

type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

When being called from code, all you need is the type name because the compiler already knows which assemblies are in there. From config, you've got to help the config system figure it out.

Typically, you don't need to specify the full assembly name. You can just give the file name and leave out the version, culture, and public key token. So if your types are both in MyApp.exe, your config needs to look like this:

<configSections>
    <section name="unity"
                 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <unity>
    <containers>
      <container >
        <types>
          <type type="UnityTest.IMyInterface, MyApp" mapTo="UnityTest.MyClass, MyApp" />
        </types>
      </container>
    </containers>
  </unity>

(I've bolded the changes).

Just put in the appropriate assembly names and you should be good to go.

-Chris
Oct 29, 2008 at 11:45 PM
It worked! Thanks a lot!

Belkys