The parameter productName could not be resolved when attempting to call constructor error

May 4, 2010 at 6:33 AM

Hi,

Below is the code I'm using:

 

public interface IProduct
{
    string Name { get; set; }
    string WriteProductDetails();
}

public class Product4 : IProduct
{
    public string Name { get; set; }
    public ILogger[] Loggers { get; set; }

    public Product4(String productName, ILogger[] loggers)
    {
        Name = productName;
        Loggers = loggers;
    }

    public string WriteProductDetails()
    {
        StringBuilder productDetails = new StringBuilder();
        productDetails.AppendFormat("{0}<br/>", Name);
        for (int i = 0; i < Loggers.Count(); i++)
        {
            productDetails.AppendFormat("{0}<br/>", Loggers[i].WriteLog());
        }
            
        return productDetails.ToString();
    }
}

 

My config file has the following information:

<!--System Types-->
<typeAlias alias="string" type="System.String, mscorlib" />

<!--Custom interfaces-->
<typeAlias alias="IProduct" type="ProductModel.IProduct, ProductModel"/>
<typeAlias alias="ILogger" type="ProductModel.ILogger, ProductModel"/>
<typeAlias alias="ILoggerArray" type="ProductModel.ILogger[], ProductModel" />
      
<!--Custom object types-->
<typeAlias alias="Product4" type="ProductModel.Product4, ProductModel"/>
<typeAlias alias="FileLogger" type="ProductModel.FileLogger, ProductModel"/>
<typeAlias alias="FakeLogger" type="ProductModel.FakeLogger, ProductModel"/>

<type type="ILogger" mapTo="FakeLogger" name="logger1">
  <lifetime type="singleton" />
</type>
<type type="ILogger" mapTo="FileLogger" name="logger2">
  <lifetime type="singleton" />
</type>

<!--arrays-->
<type type="IProduct" mapTo="Product4" name="ArrayProduct">
  <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <constructor>
      <param name="productName" parameterType="string">
      </param>
      <param name="loggers" parameterType="ILoggerArray">
        <array/>
      </param>
    </constructor>
  </typeConfig>
</type>

Now when I run my code as:

unityContainer.RegisterType<IProduct, Product4>();
IProduct product4 = unityContainer.Resolve<IProduct>();
productDetailsLabel.Text = product4.WriteProductDetails();

This gives me the following error:

{"Resolution of the dependency failed, type = \"ProductModel.IProduct\", name = \"\". Exception message is: The current build operation (build key Build Key[ProductModel.Product4, null]) failed: The parameter productName could not be resolved when attempting to call constructor ProductModel.Product4(System.String productName, ProductModel.ILogger[] loggers). (Strategy type BuildPlanStrategy, index 3)"}

What am I missing?

Arun

May 4, 2010 at 6:39 AM

What I'm actually trying to do is to pass a value to the productName using the config below:

<param name="productName" parameterType="string">
  <value value="Product 4" />
</param>

Please let me know.
Arun

May 5, 2010 at 9:28 AM

I got my concerns resolved. The explanation is here:

http://stackoverflow.com/questions/2771217/named-type-not-used-for-constructor-injection

Arun