container.Resolve based on interface - problem making it work

Nov 25, 2008 at 9:42 AM
Hi,

I'm trying to resolve a type based on an interface:

IUnityContainer container = new UnityContainer();
MyApp.Components.IProjectsService service = container.Resolve<MyApp.Components.IProjectsService>();

but I'm getting the following exception:

Microsoft.Practices.Unity.ResolutionFailedException was unhandled by user code
  Message="Resolution of the dependency failed, type = \"MyApp.Components.IProjectsService\", name = \"\". Exception message is: The current build operation (build key Build Key[MyApp.Components.IProjectsService, null]) failed: The current type, MyApp.Components.IProjectsService, is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3)"
  Source="Microsoft.Practices.Unity"
  TypeRequested="IProjectsService"
  StackTrace:
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name)
       at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name)
       at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t)
       at Microsoft.Practices.Unity.UnityContainerBase.Resolve[T]()
       at SyncInvokeGetProjects(Object , Object[] , Object[] )
       at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
  InnerException: Microsoft.Practices.ObjectBuilder2.BuildFailedException
       Message="The current build operation (build key Build Key[MyApp.Components.IProjectsService, null]) failed: The current type, MyApp.Components.IProjectsService, is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3)"
       Source="Microsoft.Practices.ObjectBuilder2"
       BuildKey="Build Key[MyApp.Components.IProjectsService, null]"
       ExecutingStrategyIndex=3
       ExecutingStrategyTypeName="BuildPlanStrategy"
       StackTrace:
            at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
       InnerException: System.InvalidOperationException
            Message="The current type, MyApp.Components.IProjectsService, is an interface and cannot be constructed. Are you missing a type mapping?"
            Source="Microsoft.Practices.ObjectBuilder2"
            StackTrace:
                 at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)
                 at BuildUp_MyApp.Components.IProjectsService(IBuilderContext )
                 at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
                 at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
                 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            InnerException:

I have configured unity as below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity"
              type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                 Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <typeAliases>
      <typeAlias alias="singleton"
           type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
               Microsoft.Practices.Unity" />
      <typeAlias alias="IProjectsService"
           type="MyApp.Components.IProjectsService, MyApp.Components" />
      <typeAlias alias="DefaultProjectsService"
           type="MyApp.Components.ProjectsService, MyApp.Components" />
    </typeAliases>
    <containers>
      <container>
        <types>
          <type type="IProjectsService" mapTo="DefaultProjectsService">
            <lifetime type="singleton" />
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

Can anyone spot if I'm missing something here - it seems like a basic scenario but I can't get it to work.

Best regards,
Robert Wilczynski.
Nov 25, 2008 at 10:52 AM
Hi, you should load the type mappings you created in the configuration file.


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

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









        
    
Nov 25, 2008 at 3:59 PM
Hi Sarah,

Thanks for your help - this has solved the problem. For some reason I missed that the container is not in anyway static and has to be configured each time (it seems is my responsibility to manage the lifetime of the unity container).