ResolutionFailedException when web hosted

Jan 5, 2010 at 1:50 PM

I am using Unity in WCF.
When running in a self-hosted environment (like Console or Windows Service or Windows App) everything works fine - but when I web-host my WCF service I get similar to following error whenever I try to resolve types with multiple constructors:

The type Int32 does not have an accessible constructor.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The type Int32 does not have an accessible constructor.

Source Error:

Line 69:         public static T Resolve<T>()
Line 70:         {
Line 71:             return Container.Resolve<T>();
Line 72:         }
Line 73: 


Source File: D:\Sudev\EntLib41UnityDemo\Configuration\UnityConfigurator.cs    Line: 71

Stack Trace:

[InvalidOperationException: The type Int32 does not have an accessible constructor.]
   Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context) +201
   BuildUp_System.Int32(IBuilderContext ) +95
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +306
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +119

[BuildFailedException: The current build operation (build key Build Key[System.Int32, null]) failed: The type Int32 does not have an accessible constructor. (Strategy type BuildPlanStrategy, index 5)]
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +349
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +173
   BuildUp_GatewayDemo.Entities.TestObject(IBuilderContext ) +230

[InvalidOperationException: The parameter t could not be resolved when attempting to call constructor GatewayDemo.Entities.TestObject(System.Int32 t).]
   Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForResolutionFailed(Exception inner, String parameterName, String constructorSignature, IBuilderContext context) +177
   BuildUp_GatewayDemo.Entities.TestObject(IBuilderContext ) +357
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +306
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +119

[BuildFailedException: The current build operation (build key Build Key[GatewayDemo.Entities.TestObject, null]) failed: The parameter t could not be resolved when attempting to call constructor GatewayDemo.Entities.TestObject(System.Int32 t). (Strategy type BuildPlanStrategy, index 5)]
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +349
   Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing) +102
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) +219

[ResolutionFailedException: Resolution of the dependency failed, type = "GatewayDemo.Entities.ITestObject", name = "". Exception message is: The current build operation (build key Build Key[GatewayDemo.Entities.TestObject, null]) failed: The parameter t could not be resolved when attempting to call constructor GatewayDemo.Entities.TestObject(System.Int32 t). (Strategy type BuildPlanStrategy, index 5)]
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) +279
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name) +42
   Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name) +40
   Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t) +41
   Microsoft.Practices.Unity.UnityContainerBase.Resolve() +82
   GatewayDemo.Configuration.UnityConfigurator.Resolve() in D:\Sudev\EntLib41UnityDemo\Configuration\UnityConfigurator.cs:71
   GatewayDemo.BusinessLogic.AuthenticationManager.Logon(String logonName, String password) in D:\Sudev\EntLib41UnityDemo\BusinessLogic\AuthenticationManager.cs:17
   ASP.global_asax.Application_Start(Object sender, EventArgs e) in d:\Sudev\EntLib41UnityDemo\DemoWebSite\Global.asax:9

 

I Explicitly configured unity to pickup the constructor which has desired signature:

 

<font size="2">

 

</font>

UnityConfigurator.Container.RegisterType<ITestObject, TestObject>()<font size="2">

.Configure<

</font>

InjectedMembers>()<font size="2">

.ConfigureInjectionFor<

</font>

TestObject>(new InjectionConstructor(55));

 

My Unity Config file reads like below:
<font size="2" color="#0000ff">

<?

</font>

xml version="1.0" encoding="utf-8" ?><font size="2" color="#0000ff">

<

</font>

configuration><font size="2" color="#0000ff">

<

</font>

configSections><font size="2" color="#0000ff">

<

</font>

section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><font size="2" color="#0000ff">

</

</font>

configSections><font size="2" color="#0000ff">

<

</font>

unity><font size="2" color="#0000ff">

<

</font>

typeAliases><font size="2" color="#0000ff">

<!--

</font>

Lifetime manager types --><font size="2" color="#0000ff">

<

</font>

typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/><font size="2" color="#0000ff">

<

</font>

typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/><font size="2" color="#0000ff">

<!--

</font>

Custom object types --><font size="2" color="#0000ff">

<

</font>

typeAlias alias="ITestObject" type="GatewayDemo.Entities.ITestObject, Entities" /><font size="2" color="#0000ff">

<

</font>

typeAlias alias="TestObject" type="GatewayDemo.Entities.TestObject, Entities" /><font size="2" color="#0000ff">

<

</font>

typeAlias alias="IAuthenticationManager" type="GatewayDemo.Interfaces.BusinessLogic.IAuthenticationManager, Interfaces"/><font size="2" color="#0000ff">

<

</font>

typeAlias alias="AuthenticationManager" type="GatewayDemo.BusinessLogic.AuthenticationManager, BusinessLogic"/><font size="2" color="#0000ff">

</

</font>

typeAliases><font size="2" color="#0000ff">

<

</font>

containers><font size="2" color="#0000ff">

<

</font>

container name="default"><font size="2" color="#0000ff">

<

</font>

types><font size="2" color="#0000ff">

<

</font>

type type="ITestObject" mapTo="TestObject"/>

<type type="IAuthenticationManager" mapTo="AuthenticationManager"/><font size="2" color="#0000ff">

</

</font>

types><font size="2" color="#0000ff">

</

</font>

container><font size="2" color="#0000ff">

</

</font>

containers><font size="2" color="#0000ff">

</

</font>

unity><font size="2" color="#0000ff">

</

</font>

configuration>

 

I'm able to resolve the IAuthenticationManager type but not the ITestObject.

Jan 5, 2010 at 1:55 PM

Oops, some HTML format didn't work out :

My Configuration File reads as follows:

<?xml version="1.0" encoding="utf-8" ?>
<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"/>

      <!-- Custom object types -->
      <typeAlias alias="ITestObject" type="GatewayDemo.Entities.ITestObject, Entities" />
      <typeAlias alias="TestObject" type="GatewayDemo.Entities.TestObject, Entities" />

      <typeAlias alias="IAuthenticationManager" type="GatewayDemo.Interfaces.BusinessLogic.IAuthenticationManager, Interfaces"/>
      <typeAlias alias="AuthenticationManager" type="GatewayDemo.BusinessLogic.AuthenticationManager, BusinessLogic"/>
    </typeAliases>

    <containers>
      <container name="default">
        <types>
          <type type="ITestObject" mapTo="TestObject">
            <lifetime type="external" />
          </type>
          <type type="IAuthenticationManager" mapTo="AuthenticationManager"/>
        </types>
      </container>
    </containers>
  </unity>

</configuration>
I've configured injection constructor the type using following syntax:
            UnityConfigurator.Container.RegisterType<ITestObject, TestObject>()
                .Configure<InjectedMembers>()
                .ConfigureInjectionFor<TestObject>(new InjectionConstructor(55));

Jan 5, 2010 at 5:17 PM

Hrm. At what point do you load the configuration into the container? That could affect things. It's obviously not seeing your selected constructor; has the container initialization logic moved between the various environments?

Also, in your explicit configuration, there's a much more straightforward way to do it:

 

Container.RegisterType<ITestObject, TestObject>(
    new InjectionConstructor(55));

 

 

Jan 6, 2010 at 3:55 AM

Thanks for your prompt response.

I load configuration into the container during Application Start Event in Global.asax. I.e. very first thing once my web application starts.

Container initialization logic hasn't been moved between both the environment. I've uploaded the code at http://www.sudev.org/EntLib41UnityDemo.zip for your reference.

Also I already tried the explicit configuration as you suggested but it gave me the same result.

Thanks Again.

Sudev Gandhi