Question

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?

Was it helpful?

Solution

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" />
            ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top