views:

384

answers:

1

I'm working on my 1st project using MS Unity IoC framework.

If I have this in my unity configuration:

  <container name="TestBusiness">
    <types>
      <type type="PFServer.DataAccess.TestDataAccess" />

Then I get the error:

Could not load type 'PFServer.DataAccess.TestDataAccess' from assembly 'Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

However, if I add the assembly name to the type definition:

  <container name="TestBusiness">
    <types>
      <type type="PFServer.DataAccess.TestDataAccess, PFServer" />

Then it works fine. Is there any way to add some default set of assemblies to load types from? The odd thing in this case is that "PFServer.dll" is the assembly that contains all this configuration anyway. I find it odd that the current assembly isn't in the path to resolve objects... Or am I just doing something wrong?

+2  A: 

Unity resolves strings to Type objects by calling the static method Type.GetType(string) which expects an argument that is an AssemblyQualifiedName. The call to GetType is made in the Microsoft.Practices.Unity.Configuration assembly which does not know about your PFServer assembly so it needs the assembly name in the string.

I think type aliases might help you. Here's an example.

<unity>
    <typeAliases>
        <typeAlias alias="TestDataAccess" type="PFServer.DataAccess.TestDataAccess, PFServer" />
        ...
    </typeAliases>

    <containers>
      <container name="TestBusiness">
        <types>
            <type type="TestDataAccess" />
            ...
Lee
Thanks for the feedback. I was looking over the documentation for Unity on MSDN, and in their examples there are places where they don't specifically add the assembly name, which is why I was confused that I had to. Its not the first mistake I've seen in the unity documentation and examples though.
rally25rs