Problem with configuration Unity 2.0

Aug 2, 2010 at 8:07 AM

Hello all,

I'm trying to set up unity to handle interface to class resolves for a few libraries (with independent Unity config files).

My config looks 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">
    <assembly name="AAA.Data"/>
    <namespace name="AAA.Data" />
    <container>
      <register type="IBbbDefinitionFinder" mapTo="CBbbDefinitionFinder" />
      <register type="IBbbDefinition" mapTo="CBbbDefinition"/>
      <register type="IBbbFieldDefinitionFinder" mapTo="CBbbFieldDefinitionFinder"/>
      <register type="IBbbFieldDefinition" mapTo="CBbbFieldDefinition"/>
      <register type="IBbbRelationKeyFinder" mapTo="CBbbRelationKeyFinder"/>

      <register type="IBbbRelationKey" mapTo="CBbbRelationKey" />
      <register type="IBbbSourceFinder" mapTo="CBbbSourceFinder"/>
      <register type="IBbbSource" mapTo="CBbbSource"/>
      <register type="IBbbImportFinder" mapTo="CBbbImportFinder"/>
      <register type="IBbbImport" mapTo="CBbbImport"/>
    </container>
  </unity>
</configuration>

This is the way i try to load it, from within the AAA.Data library:

var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AAA.Data.Unity.config");
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection("unity");
var myContainer = new UnityContainer();
section.Configure(myContainer);
UnityContainer = myContainer;

And this is the exception i'm getting:

System.InvalidOperationException was unhandled by user code
  Message="The type name or alias CBbbDefinitionFinder could not be resolved. Please check your configuration file and verify this type name."
  Source="Microsoft.Practices.Unity.Configuration"
  StackTrace:
       bei Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:Zeile 110.
       bei Microsoft.Practices.Unity.Configuration.RegisterElement.GetMappedType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:Zeile 132.
       bei Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:Zeile 89.
       bei Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:Zeile 114.
       bei Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Utility\EnumerableExtensions.cs:Zeile 36.
       bei Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:Zeile 110.
       bei Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityConfigurationSection.cs:Zeile 152.
       bei AAA.Data.CBbbFactory..ctor() in ...
       bei BuildUp_Ikaros.Selector.Data.CDataFactory(IBuilderContext )
       bei Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:Zeile 43.
       bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:Zeile 87.
  InnerException: 

Would anyone have an idea what is wrong with my config?

Thanks in advance

Aug 2, 2010 at 8:36 AM

The obvious question would be, does the CBbbDefinitionFinder exists under the namespace AAA.Data and inside the AAA.Data assembly?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 2, 2010 at 8:40 AM

Hello Sarah,

yes, it definitly does - should have mentioned that this config and code is IN the assembly AAA.Data, plus a copy of it in the ASP.NET web folder the application is running in, and by looking into the "section" variable it seems to be loaded too.

The code snippet is in a factory class resolving the instances for the rest of the application.

Regards

Ulli

Aug 2, 2010 at 8:58 AM

Does the executable project references the AAA.Data assembly?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 2, 2010 at 9:00 AM

No, it doesn't directly. The executing project references a presenter assembly, which in turn references the AAA.Data assembly.

Is that a problem? We wanted to keep the references per assembly as small as possibel.

Regards

Ulli

Aug 2, 2010 at 9:10 AM

I suspect that is the problem, it can't load the AAA.Data assembly.  It's not really necessary to create a reference, you just need to have the assembly in a location where the host project can find it.  Deploy the AAA.Data assembly to output folder of the executable project and see if that is really the problem.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 2, 2010 at 10:50 AM

Hello Sarah,

no, that wasn't the problem but your tip pointed me in the right direction.

I ended up modifying some implementations of generic classes that were causing the problems, now i only need to figure out how to automatically inject the unity container when i get objects from non-unity-based business classes.
Right now it seems I have to do that manually, well, if i can't find a better way then thats the way it will be.

Thanks for your help

Ulli