views:

395

answers:

1

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

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <!--Unity Configuration-->
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
           Microsoft.Practices.Unity.Configuration"/>
    </configSections>

    <unity>
        <containers>

            <container>

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

            </container>

        </containers>
    </unity>

</configuration>

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 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)

+1  A: 

I'm not sure that this would trip Unity up, but what happens if you remove the Calculate and CalculationHistory properties from CalculateApi? These are hiding the members of the base class, which is almost certainly not what you want to be doing.

I always configure in code, rather than XML, so I'm not sure about this, but maybe try removing the name attribute from your mapping, or passing the proper name when you call Resolve -- I'm guessing something like container.Resolve<BaseCalculatorServer>("CalculatorServer"). Unless you are registering multiple implementations, though, I'd remove the name altogether.

Jay
Removing the Properties and cleaning my build and running again seems to have worked for whatever reason. So woot to that. Thanks
Terrance