Unity The parameter host could not be resolved when attempting to call constructor

Posted by Terrance on Stack Overflow See other posts from Stack Overflow or by Terrance
Published on 2010-04-01T19:02:35Z Indexed on 2010/04/01 19:13 UTC
Read the original article Hit count: 519

Filed under:
|
|
|
|

When I attempt to instantiate my instance of the base class I get the error:

a ResolutionFailedException with roughly the following error "The parameter host could not be resolved when attempting to call constructor"

I'm currently not using an Interface for the base type and my instance of the class is inheriting the base type class. I'm new to Unity and DI so I'm thinking its something I forgot possibly.

        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = "Unity.Config";
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map,  ConfigurationUserLevel.None);
        UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("unity");
        IUnityContainer container = new UnityContainer();
        section.Containers.Default.Configure(container);
        //Throws exception here on this
        BaseCalculatorServer server = container.Resolve<BaseCalculatorServer>();

and the Unity.Config file

  <container>

    <types>
      <type name ="CalculatorServer" type="Calculator.Logic.BaseCalculatorServer, Calculator.Logic" mapTo="Calculator.Logic.CalculateApi, Calculator.Logic"/>
    </types>

  </container>

</containers>

The Base class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Transactions;
using Microsoft.Practices.Unity;
using Calculator.Logic;

namespace Calculator.Logic
{

    public class BaseCalculatorServer : IDisposable
    {
        public BaseCalculatorServer(){}
        public CalculateDelegate Calculate { get; set; }
        public CalculationHistoryDelegate CalculationHistory { get; set; }


        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or     resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose();
        }
    }
}

The Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calculator.Logic;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using Microsoft.Practices.Unity;

namespace Calculator.Logic
{
    public class CalculateApi:BaseCalculatorServer
    {
        public CalculateApi(ServiceHost host)
        {
            host.Open();
            Console.WriteLine("Press Enter To Exit");
            Console.ReadLine();
            host.Close();
        }
        public CalculateDelegate Calculate { get; set; }
        public CalculationHistoryDelegate CalculationHistory { get; set; }
    }
}

Yes both base class and implementation are in the same Namespace and thats something design wise that will change once I get this working. Oh and a more detailed error

Resolution of the dependency failed, type = "Calculator.Logic.BaseCalculatorServer", name = "". Exception message is: The current build operation (build key Build Key[Calculator.Logic.BaseCalculatorServer, null]) failed: The value for the property "Calculate" could not be resolved. (Strategy type BuildPlanStrategy, index 3)

© Stack Overflow or respective owner

Related posts about dependency-injection

Related posts about unity