ResolutionFailedException: The current type is an interface and cannot be constructed.

Jun 1, 2010 at 9:35 PM
Hi, 
I have started using Unity only over the past week. Please help me as I am not able to resolve the issue. 

I use the following configuration in App.config, both in my Actual Code and my Unit Tests. 

In my ClassInitialize , after loading the Unity Container, when I  try to resolve the interface, 
IDatabaseAdapter adapter = unityContainer.Resolve<IDatabaseAdapter>();  I receive a ResolutionFailedException : 

The current type, VMS.VC.Adapters.IDatabaseAdapter, is an interface and cannot be constructed. 
Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3) --->  
Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[VMS.VC.Adapters.IDatabaseAdapter, null]) failed: 
The current type, VMS.VC.Adapters.IDatabaseAdapter, is an interface and cannot be constructed. 
Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3) --->  System.InvalidOperationException: 
The current type, VMS.VC.Adapters.IDatabaseAdapter, is an interface and cannot be constructed. Are you missing a type mapping?.

Similarly, 
On trying to resolve the Abstract class with the correct derived class, 
AbstractBuilderCache _builderCache = unityContainer.Resolve<AbstractBuilderCache>(); 

I receive the exception : 
The current build operation (build key Build Key[Adapters.AbstractBuilderCache, null]) failed: The type AbstractBuilderCache does not have an accessible constructor.

All the assemblies are referenced by the UT projects too.  What should I do?

I use Property Injection in my code and do not provide Constructor Injection. 

I appreciate your help in advance. Thank you. 

<configuration>
  <configSections>
    <section name="unity"
              type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                 Microsoft.Practices.Unity.Configuration, Version=1.2.0.0,
                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <unity>
    <typeAliases>

      <!--Lifetime manager types-->
      <typeAlias alias="singleton"
           type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
               Microsoft.Practices.Unity" />
      <typeAlias alias="external"
           type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,
               Microsoft.Practices.Unity" />
      <!--User-defined type aliases-->

      <!-- Interfaces-->
      <typeAlias alias ="OISAdapterInterface"
                     type =" Adapters.IDatabaseAdapter,  DataInterfaces"/>

      <typeAlias alias ="AbstractBuilderCache"
                 type =" Adapters.AbstractBuilderCache,  DataInterfaces"/>


      <!-- Implementations-->
      <typeAlias alias ="SRSOISAdapter"
                    type =" Adapters.MVDSAdapter,  DatabaseAdapter"/>

      <typeAlias alias ="BuilderCache"
                 type =" Adapters.BuilderObjectCache,  DataInterfaces"/>

      <containers>
        <container name ="SRS">
          <types>
            <type type="OISAdapterInterface" mapTo="SRSOISAdapter">
              <lifetime type="singleton" />
            </type >
  <type type="AbstractBuilderCache" mapTo="BuilderCache">
            <lifetime type="singleton" />
          </type>
          </types>
        </container>
      </containers>
    </unity >
</configuration>



<!--Lifetime manager types--> <!--User-defined type aliases--> <!-- Interfaces--> <!-- Implementations-->
Jun 2, 2010 at 12:03 AM
Edited Jun 2, 2010 at 2:33 AM

The type attribute element should be a fullly qualified name; that is namespace.classname, assemblyname.  So it should be something like type="VMS.VC.Adapter.IDatabaseAdapter, assemblyName", (I'm not sure what your assembly name is.)  Modify your other typeAliases as well.

Also make sure that your BuilderObjectCache class has a public constructor. 

 

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

Jun 2, 2010 at 6:04 PM

Thank you Sarah for the reply. 

I do have the fully qualified namespace in my actual configuration.

Is it necessary to provide an explicit public constructor? Would the default constructor not work?

The BuilderCache object only has properties and no constructors (private , protected, static or public) explicitly defined. 

 

 

 

 

 

Jun 2, 2010 at 11:25 PM

It's not necessary to provide an explicit default constructor, if you don't have any other constructors, it should work.

Do you have other containers in your actual config? You might not be loading the correct container, could you post your code for that?

 

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

Jun 3, 2010 at 3:51 AM

How are you creating and configuring the container? Are you sure it's loading the configuration you think it is?

 

Oct 16, 2014 at 8:23 AM
Edited Oct 16, 2014 at 9:01 AM
I had similar issue which solved after adding an alias to concrete type e.g.
<alias alias="IMyType" type="MyTypeLib.IMyType, MyTypeLib" />
<alias alias="MyConcreteType" type="MyConcreteTypeApp.MyConcreteType, MyConcreteTypeApp" />
<containers>
  <container>
    <register type="IMyType" mapTo="MyConcreteType" />
  </container>
</containers>